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.

Recommended Answers

All 2 Replies

try raw_input instead of input

Thanks a lot, that solved it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.