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

0 投票
2 回答
5068 浏览
提问于 2025-04-18 02:30

我该如何把多个输入放进一个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”时,它就什么都不打印,所以不会退出。

顺便说一下:我在例子里做的那样是没效果的。

2 个回答

1

我想你是想表达类似这样的意思——你可以大大简化你的代码。

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:语句。elif只有在前面的条件没有被执行时才会被检查,所以在这里非常合适。考虑到这一点,else也只需要写一次就可以了。

3

在我的一个程序中,我设计了一种更复杂的选择选项的方法。这个方法是把每个选项都和一个特定的功能联系起来。

想象一下,我希望用户从这些功能中选择一个:

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

def say_hello():
    print "Hello world!"

def etcetera():
    pass

我创建了一个字典,字典的键是用户输入的关键词,值则是一个描述和一个功能。在这个例子中,我使用的是字符串数字。

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

编辑 另外,你可以设置一个返回关键词,这样就可以有嵌套菜单了。

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

撰写回答