Why doesn't this loop?

this is my code

def write():
	code = input()
	code = code.lower()
	codetable = code.split()
	function = codetable[1]
	afterfunction = codetable[2]
	afterfunction2 = codetable[3]
	afterfunction3 = codetable[4]
	afterfunction4 = codetable[5]

while True:
	write()
	if function == "local":
		if afterfunction2 == "=":
			if afterfunction3.isnumeric() == True:
				afterfunction = int(afterfunction3)
			elif afterfunction3 == "true":
				afterfunction = True
			elif afterfunction3 == "false":
				afterfunction = False
			else:
				afterfunction = afterfunction3

		elif afterfunction2 == None:
			afterfunction = None

it was supposed to keep wanting answers, but it doesnt.
since this is in a .py file it simply crashes.
it doesn’t give any error

Double check how you’re running your code, specifically that you are able to see the output and that you’re providing some input to stdin.

This code should crash with a detailed exception traceback that will tell you what problem occured and where. Specifically, it looks like it should either be crashing with an IndexError (if the user input was bad), or with a NameError if the input was OK and your script makes it to the line if function == "local":. At that point, you’re referencing a variable that isn’t defined (function only exists inside the call to write).

See also

2 Likes

If you want variables defined inside a function to be accessible outside, you have to return them. Otherwise they only exist inside the function.

For example, this doesn’t work

def foo():
    hello = "hello"

foo()
print(hello)  # ERROR

Instead, do this

def foo():
    hello = "hello"
    return hello  # return the variable

hello = foo()  # capture the return
print(hello)  # okay

You can also return multiple variables.

def foo():
    hello = "hello"
    world = "world"
    return hello, world

hello, world = foo()
1 Like

it gives no error, i tested on python (not .py file) and it doesnt give error but it also doesnt loop

but well
now i have another problem with loops

if app == "binário":
	input = int(input("Qual é o número decimal que queres transformar em binário?: "))
	b = 1
	while input != 0:
		c = input % 2
		d = c*b
		e = e+d
		input = input//2
		b = b*10
		print(f"binário = binário + {c}*{b}")
		print(f"binário = binário + {d}")
		wait(0.1)
	else:
		input("Toca no enter para saĂ­r: ")

this is just a bit of a code i made
the app is just a input and then its lowered
but it doesnt loop

Where does the value of app come from?

It probably does not reference any value and certainly not “binário”. if it isn’t, your programm just ends without showing anything.

I created a file binario.py with the following contents:

input = int(input("Qual é o número decimal que queres transformar em binário?: "))
b = 1
while input != 0:
	c = input % 2
	d = c*b
	e = e+d
	input = input//2
	b = b*10
	print(f"binário = binário + {c}*{b}")
	print(f"binário = binário + {d}")
	wait(0.1)
else:
	input("Toca no enter para saĂ­r: ")

Executing this with input “0” returns an error:

(.venv) mennoh@192:~/python/test1> python ./binario.py
Qual é o número decimal que queres transformar em binário?: 0
Traceback (most recent call last):
  File "/home/mennoh/python/test1/./binario.py", line 13, in <module>
    input("Toca no enter para saĂ­r: ")
TypeError: 'int' object is not callable

With other input it shows:

(.venv) mennoh@192:~/python/test1> python ./binario.py
Qual é o número decimal que queres transformar em binário?: 8
Traceback (most recent call last):
  File "/home/mennoh/python/test1/./binario.py", line 6, in <module>
    e = e+d
        ^
NameError: name 'e' is not defined

You have some work to do.

In order for us to help you, we need to be able to see the problem just as you experience it, by using the code that you provide. We can’t see your computer screen or anything that you are typing in. It’s not always possible to just copy and paste some section of the program, where you think the problem is, and make it clear. We need to be able to copy and paste what you gave us, without adding or changing anything, and see the problem.

So, before posting, you should make sure that this is possible. Copy and paste the code back from your post, in a new file, and save and run it. Make sure that it shows the exact problem that you want to ask about, and try to make it as simple as you can while still showing that problem. Then, when someone answers, try to fix that example first, and then apply that advice to the real program.

Some more hints on how to do this:

2 Likes

Just looking at this bit:

 if app == "binário":
     input = int(input("Qual é o número decimal que queres transformar em binário?: "))
     b = 1
     while input != 0:

You are storing the output of the input() function in the variable
input. This won’t work. Once you’ve done that the name input is no
longer bound to the input() function.

Choose another name for your variable.

after some work and openning the .py file a lot of times, i discovered some things… some very weird things… one of the problems was the wait(0.1) it gave error, but now, after deleting it, if i input anything, the output is 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
i think i need even more work :confused: