将多个输入分配到一个if/else语句中。[PYTHON]
如何将多个输入分配到一个if/else语句中 例如:将多个输入分配到一个if/else语句中。[PYTHON],python,choice,Python,Choice,如何将多个输入分配到一个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 (" ") #
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")
所以是的,基本上我想做的是设置多项选择选项,所以当你在我选择框中写退出时,它将退出,当你写停留时,它将不打印任何内容,所以它不会退出
顺便说一句:当我做我在示例中所做的事情时,它不起作用。我想你的意思是这样的-你可以将代码简化很多
if choose == "quit":
quit() # Notice the brackets to call the function.
elif choose == "stay":
print (" ")
else:
print ("What is that")
在上面的例子中,我修改了,所以当输入'quit'时,调用函数quit()
,程序退出。实际上,它只会打印函数对象的字符串表示形式
此外,如果:。。。其他:语句使用
elif
。只有在没有执行前面的条件语句时,才会检查此选项,因此在这里它是完美的。考虑到这一点,只需执行一次其他。我认为您的意思是这样的-您可以将代码简化很多:
if choose == "quit":
quit() # Notice the brackets to call the function.
elif choose == "stay":
print (" ")
else:
print ("What is that")
在上面的例子中,我修改了,所以当输入'quit'时,调用函数quit()
,程序退出。实际上,它只会打印函数对象的字符串表示形式
此外,如果:。。。其他:
语句使用elif
。只有在没有执行前面的条件语句时,才会检查此选项,因此在这里它是完美的。考虑到这一点,只需执行一次其他。我认为您的意思是这样的-您可以将代码简化很多:
if choose == "quit":
quit() # Notice the brackets to call the function.
elif choose == "stay":
print (" ")
else:
print ("What is that")
在上面的例子中,我修改了,所以当输入'quit'时,调用函数quit()
,程序退出。实际上,它只会打印函数对象的字符串表示形式
此外,如果:。。。其他:
语句使用elif
。只有在没有执行前面的条件语句时,才会检查此选项,因此在这里它是完美的。考虑到这一点,只需执行一次其他。我认为您的意思是这样的-您可以将代码简化很多:
if choose == "quit":
quit() # Notice the brackets to call the function.
elif choose == "stay":
print (" ")
else:
print ("What is that")
在上面的例子中,我修改了,所以当输入'quit'时,调用函数quit()
,程序退出。实际上,它只会打印函数对象的字符串表示形式
此外,如果:。。。其他:
语句使用elif
。只有在没有执行前面的条件语句时,才会检查此选项,因此在这里它是完美的。考虑到这一点,只需做一次其他。在我的一个程序中,我用了一种更复杂的方法来选择选项。
它涉及将每个选项链接到特定功能
假设我希望用户选择以下功能之一:
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
>>>
编辑
或者,您可以创建一个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
>>>
在我的一个程序中,我用了一种更复杂的方法来选择选项。
它涉及将每个选项链接到特定功能
假设我希望用户选择以下功能之一:
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
>>>
编辑
或者,您可以创建一个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
>>>
在我的一个程序中,我用了一种更复杂的方法来选择选项。
它涉及将每个选项链接到特定功能
假设我希望用户选择以下功能之一:
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
>>>
编辑
或者,您可以创建一个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
>>>
在我的一个程序中,我用了一种更复杂的方法来选择选项。
它涉及将每个选项链接到特定功能
假设我希望用户选择以下功能之一:
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
>>>
编辑
或者,您可以创建一个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
>>>
嗯。。。不,我正在尝试做多项选择,因此likequit=退出程序stay=不打印任何内容,以便stays@AlexThornton请不要催他接受你的答案,其他人可能会想出更好的答案。当然,添加无数的elif
语句并不是解决问题的最佳方法go@AlexThornton尽管如此,它还是试图提供高质量的代码,让他(一小时内两次)接受你的答案只会降低OP获得最佳答案的机会。至少给一两天,如果没有人有更好的答案,那么让OP接受你的答案,如果他还没有这样做。呃。。。不,我正在尝试做多项选择,因此likequit=退出程序stay=不打印任何内容,以便stays@AlexThornton请不要催他接受你的答案,其他人可能会想出更好的答案。当然,添加无数的elif
语句并不是解决问题的最佳方法go@AlexThornton尽管如此,它还是试图提供高质量的代码,让他(一小时内两次)接受你的答案只会降低OP获得最佳答案的机会。如果没有更好的answe,至少给它一两天时间