We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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.

2
Contributors
2
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
KatseasSAvvas
Newbie Poster
5 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

try raw_input instead of input

ihatehippies
Junior Poster
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

Thanks a lot, that solved it.

KatseasSAvvas
Newbie Poster
5 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by ihatehippies

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0809 seconds using 2.69MB