Reputation: 55
I would like my program to no nothing if user selects option in menu that does not exist.
def mainMenu():
os.system("clear")
print("menu")
print("1 - option 1")
print("2 - option 2")
selection=str(raw_input(""))
if selection=='1':
some super-interesting things
if selection=='2':
Kim Kardashian with Eskimo riding a polar bear
else:
literally DO NOTHING, no changes, no exiting the program, just takes the input and waits for another command
mainMenu()
How to achieve that? 'pass' or 'return' causes to exit the program. 'mainMenu()' causes refreshing menu "page"
Upvotes: 0
Views: 5355
Reputation: 1392
I would just use a loop that keeps waiting for valid input. It requests the input and if it's not one of the valid options it will keep waiting input.
def mainMenu():
os.system("clear")
print("menu")
print("1 - option 1")
print("2 - option 2")
valid_options = ['1', '2']
while True:
selection = str(raw_input(""))
if selection in valid_options:
break
if selection == '1':
some super-interesting things
elif selection == '2':
Kim Kardashian with Eskimo riding a polar bear
mainMenu()
You could even add an else statement in the if inside the loop to request the user for a valid option.
Upvotes: 3
Reputation: 77902
A 'pass' or 'return' statement doesn't actually "exit the program", it exits the function. Now since there's nothing else in your program, it also ends up exiting the program, but this is not by itself due to those statements.
If your goal is to have the mainMenu function "restarting" when selection is neither 1 nor 2, you need a loop:
def mainMenu():
while True:
os.system("clear")
print("menu")
print("1 - option 1")
print("2 - option 2")
# you need to leave a way for the user
# to exit the program
print("0 - exit")
# raw_input already returns a string,
# but you may want to remove unwanted whitespace chars
selection = raw_input("").strip()
if selection == '0':
return
elif selection == '1':
some_super_interesting_things()
elif selection == '2':
KimKardashian().with(Eskimo()).riding(a_polar_bear)
else:
# no strictly needed but the users will appreciate...
print("sorry, {} is not a valid choice".format(selection))
# this prevents this code from being executed if you
# try to import your script as a module
if __name__ == "__main__":
mainMenu()
Upvotes: 0
Reputation: 1598
Erasing the else
statement should do the job:
def mainMenu():
os.system("clear")
print("menu")
print("1 - option 1")
print("2 - option 2")
selection=str(raw_input(""))
if selection=='1':
some super-interesting things
if selection=='2':
Kim Kardashian with Eskimo riding a polar bear
mainMenu()
Anyway, I understand this is far from a final code, obviously the code inside if conditions won't work. If you want the code to ask again, it should be inside a loop, otherwise, the program will end. It should look like this:
def mainMenu():
os.system("clear")
print("menu")
print("1 - option 1")
print("2 - option 2")
selection = '0'
while selection != '1' or selection !='2':
selection=str(raw_input(""))
if selection=='1':
some super-interesting things
if selection=='2':
Kim Kardashian with Eskimo riding a polar bear
mainMenu()
Upvotes: 0