Reputation: 81
I've added in this function which makes sure the user actually wants to quit the program. It works when you actually want to quit but if you want to return to the program it just loops the statement:
def WantToQuit():
Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ")
if Quit == 'y':
print ('')
elif Quit == 'n':
DisplayMenu()
return WantToQuit()
Elsewhere:
elif Choice == 'q':
WantToQuit()
raw_input('Press enter key to continue ')
Upvotes: 0
Views: 3326
Reputation: 321
Instead of making a new function, you can use an already defined function called quit()
. The quit
function will pop up a box that says:
[In]
quit()
[Out]
Your program is still running!
Do you want to kill it?
Yes No
Upvotes: 0
Reputation: 30210
It's hard to show you what you're doing wrong without more code, but here's how I figure you have it setup:
def WantToQuit():
Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ")
if Quit == 'y':
print ('')
elif Quit == 'n':
DisplayMenu()
return WantToQuit()
while(True):
DisplayMenu()
# Some logic to get input and handle it
# For example, something like
selection = raw_input("Please make a selection: ")
if(selection == "1"):
doSomething()
elif(selection == "2"):
doSomethingElse()
elif(selection == "q"):
WantToQuit()
else:
# TODO: Handle this !
pass
This is how I would do it:
def WantToQuit():
Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ")
if Quit == 'y':
return true
elif Quit == 'n':
return false
else:
# TODO: Handle this !
pass
while(True):
DisplayMenu()
# Some logic to get input and handle it
# For example, something like
selection = raw_input("Please make a selection: ").lower()
if(selection == "1"):
doSomething()
elif(selection == "2"):
doSomethingElse()
elif(selection == "q"):
if(WantToQuit()): break
else:
# TODO: Handle this !
pass
Alternatively, you could do something like:
def WantToQuit():
Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ")
if Quit == 'y':
sys.exit(0)
elif Quit == 'n':
return # Do nothing really
else:
# TODO: Handle this !
pass
while(True):
DisplayMenu()
# Some logic to get input and handle it
# For example, something like
selection = raw_input("Please make a selection: ").lower()
if(selection == "1"):
doSomething()
elif(selection == "2"):
doSomethingElse()
elif(selection == "q"):
WantToQuit()
else:
# TODO: Handle this !
pass
The first example has the WantToQuit
function return a boolean whether the user actually wants to quit. If so, the infinite loop is broken and the program naturally exits.
The second example handles the exiting inside the WantToQuit
function, calling sys.exit()
to exit immediately.
The first is probably preferable, although both are used in practice.
Upvotes: 1
Reputation: 26352
Replace print ('')
with sys.exit (0)
with and remove the return WantToQuit()
.
I would also recommend that you apply .lower()
on your Quit
variable so that it is case-insensitive.
Upvotes: 5