1.11M Members

While error: NameError: name 'n' is not defined

 
0
 

I'd like to ask the user if he/she wants to rerun a script. I've thought of doing that with a while. My problem is that both

import sys
rerun = "y"
while rerun == "n":
    sys.exit()  
else:
    print("do some stuff")
    rerun = str(input("Would you like to start again? [y/n]: "))

and

import sys
rerun = "y"
while rerun == "y":
    print("do some stuff")
    rerun = str(input("Would you like to start again? [y/n]: "))
else:
    sys.exit()

when answered as "n" return

Would you like to start again? [y/n]: n
Traceback (most recent call last):
File "/usr/local/bin/thisscript.py", line 148, in <module>
rerun = str(input("Would you like to start again? [y/n]: "))
File "<string>", line 1, in <module>
NameError: name 'n' is not defined

I'm testing this at a Ubuntu Linux box. I've set the script as executable and set the shebang as #!/usr/bin/env python if that changes anything. The script seems to work fine at IDLE's Python shell.

 
1
 

try raw_input instead of input

 
0
 

Thanks a lot, that solved it.

Question Answered as of 2 Years Ago by ihatehippies
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: