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.

Edited 4 Years Ago by KatseasSAvvas: n/a

This question has already been answered. Start a new discussion instead.