将多个输入分配到一个if/else语句中,[PYTHON]

34gzjxbg  于 2023-01-04  发布在  Python
关注(0)|答案(2)|浏览(108)

如何将多个输入赋值给一个if/else语句
示例:

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")

是的,基本上我要做的是设置多项选择,当你在我选择框中写quit时,它会退出,当你写stay时,它不会打印任何东西,所以它不会退出。
顺便说一句:当我做我在例子中做的事情时,它不起作用。

bnlyeluc

bnlyeluc1#

在我的一个程序中,我设计了一个更复杂的选项选择方法,它涉及到每个选项都与一个特定的函数相关联。
假设我希望用户选择以下函数之一:

def Quit():
    print "goodbye"
    os._exit(1)

def say_hello():
    print "Hello world!"

def etcetera():
    pass

我做了一个字典,key是用户输入的关键字,value是描述和函数,这里我使用字符串数字

OPTIONS = {"0":dict( desc = "Quit", func = Quit), 
           "1":dict( desc = "Print hello", func = say_hello), 
           "2":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"]()

>>>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或者,您可以创建一个return关键字,这样您就可以拥有嵌套菜单。

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
>>>
5anewei6

5anewei62#

我想你的意思大概是这样的--你可以大大简化你的代码。:

if choose == "quit":
    quit()  # Notice the brackets to call the function.
elif choose == "stay":
    print (" ")
else:
    print ("What is that")

在上面的例子中,我做了修改,当输入'quit'时,函数quit()被调用,程序退出,因为它只打印了函数对象的字符串表示。
此外,您可以使用elif合并条件if: ... else:语句。只有在前面的条件语句没有执行时才检查,因此在这里它是完美的。记住这一点,也只需要执行else一次。

相关问题