1,105,578 Community Members

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

Member Avatar
KatseasSAvvas
Newbie Poster
5 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
ihatehippies
Junior Poster
199 posts since Oct 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 2 [?]
 
1
 

try raw_input instead of input

Member Avatar
KatseasSAvvas
Newbie Poster
5 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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: