Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with a simple code
#1
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
Reply
#2
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
Recommended Tutorials:
Reply
#3
well that was a terrible mistake than thanks a lot!!!
Reply
#4
dont worry about it. It happens a lot.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 340 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 494 Nov-07-2023, 04:32 PM
Last Post: snippsat
  A simple problem, how best to solve it? SuchUmami 2 732 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  help me simple code result min and max number abrahimusmaximus 2 916 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,464 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,248 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,820 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,375 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,697 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  My simple code don't works !! Nabi666 1 1,624 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020