Reputation: 1659
How do I assign multiple inputs into one if/else statement
Example:
print ("quit = quits the program")
print ("stay = stays in the program")
choose = input("I choose: ")
if choose == "quit":
quit
else:
print ("What is that")
if choose == "stay":
print (" ") #Prints nothing so basically its not going to quit
else:
print ("What is that")
so yeah, basically what I am trying to do is to set multiple choice option, so when you write quit in the I choose box its going to quit and when you write stay it's going to print nothing so it's not going to quit.
BTW: when I do what I did in the example it doesn't work.
Upvotes: 0
Views: 5206
Reputation: 23624
In one of my programs I made a more complex way of choosing options. It involves each option being linked to a particular function.
Imagine I wanted a user to choose one of these functions:
def Quit():
print "goodbye"
os._exit(1)
def say_hello():
print "Hello world!"
def etcetera():
pass
I make a dictionary with key the being user-input keyword, and values being a description and a function. In this case I use string numbers
OPTIONS = {"0":dict( desc = "Quit", func = Quit),
"1":dict( desc = "Print hello", func = say_hello),
"2":dict( desc = "Another example", func = etcetera)}
Then my menu function looks like this!
def main_menu():
while True:
print "\nPlease choose an option:"
for key in sorted(OPTIONS.keys()):
print "\t" + key + "\t" + OPTIONS[key]["desc"]
input = raw_input("Selection: ")
if not input in OPTIONS.keys():
print "Invalid selection"
else:
OPTIONS[input]["func"]()
>>>main_menu()
Please choose an option
0 Quit
1 Print hello
2 Another example
Selection: 1
Hello world!
Please choose an option
0 Quit
1 Print hello
2 Another example
Selection: 0
goodbye
>>>
EDIT Alternatively you could make a return keyword such that you could have nested menus.
OPTIONS = {"0":dict( desc = "Quit", func = Quit),
"1":dict( desc = "Go to menu 2", func = menu_2),
OPTIONS2 = {"1":dict( desc = "Another example", func = etcetera)}
def main_menu():
while True:
print "\nPlease choose an option:"
for key in sorted(OPTIONS.keys()):
print "\t" + key + "\t" + OPTIONS[key]["desc"]
input = raw_input("Selection: ")
if not input in OPTIONS.keys():
print "Invalid selection"
else:
OPTIONS[input]["func"]()
def main_2():
while True:
print "\nPlease choose an option :"
print "\t0\tReturn"
for key in sorted(OPTIONS2.keys()):
print "\t" + key + "\t" + OPTIONS2[key]["desc"]
input = raw_input("Selection: ")
if input == '0':
return
if not input in OPTIONS2.keys():
print "Invalid selection"
else:
OPTIONS2[input]["func"]()
>>>main_menu()
Please choose an option
0 Quit
1 Go to menu 2
Selection: 1
Please choose an option
0 Return
1 Another example
Selection: 0
Please choose an option
0 Quit
1 Go to menu 2
Selection: 0
goodbye
>>>
Upvotes: 3
Reputation: 20371
I think you mean something like this- you can simplify your code by quite a bit.:
if choose == "quit":
quit() # Notice the brackets to call the function.
elif choose == "stay":
print (" ")
else:
print ("What is that")
In the above example, I have modified so when 'quit' is inputted, the function quit()
is called so the program exits. As it was it would only have printed the string representation of the function object.
Furthermore, you can merge your conditional if: ... else:
statements using elif
. This is only checked if a preceding conditional statement was not executed, so it is perfect here. With this in mind there is also only a need to do the else
once.
Upvotes: 1