0

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.

2 Answers 2

3

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
>>>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

11 Comments

Eh... no am trying to make multiple choices so like
quit = quits the program
stay = prints nothing so that it stays
@AlexThornton please stop rushing him to accept your answer, someone else might come up with a better one. Surely adding countless elif statements is not the best way to go
@AlexThornton It seeks to provide high quality code nonetheless, asking him (twice within the hour) to accept your answer just reduces the chances of OP getting the best. At least give it a day or 2, if nobody has a better answer then ask the OP to accept yours if he hasn't yet done so.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.