Python Forum

Full Version: problem with a simple code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am new to python and I am trying out some things in python and wanted to experiment a bit with while as loop but I keep getting this error

"Traceback (most recent call last):
File "testing.py", line 4, in <module>
choose = input("go on (y) or stop (s)")
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
"

I am able to run the code but when I enter my selection it gives me the error. Is it because if I set a string once it is "forever" and I can't change it again? If it is like that how can I fix it?

Thanks

loop = 1

while loop == 1:
	choose = input("go on (y) or stop (s)")
	if choose == "y":
		print("ok")
		loop = 1
	if choose == "s":
		print("See you next time")
		loop = 0
You are using a python2.x intepreter with python3.x code. Either change input to raw_input or use a python3.x intepreter. I would suggest the latter as python2.x is pretty much on its death bed.

as a preference i would write it like this. Mainly because its more readable.
done = False
 
while not done:
    choose = input("go on (y) or stop (s)")
    if choose == "y":
        print("ok")
    elif choose == "s":
        print("See you next time")
        done = True
well that was a terrible mistake than thanks a lot!!!
dont worry about it. It happens a lot.