我写了一个菜单功能,该功能一直循环到用户按下3退出。但是,当用户通过按1或2询问其他选项时,我希望它打印一些内容。这是我的代码:
def Menu():
return("\n"
+ "Menu:\n-------------"
+ "\n1 - Does This"
+ "\n2 - Does That"
+ "\n3 - Quit"
+ "\n")
if option == 1:
return "\nThis"
if option == 2:
return "\nThat"
option = None
while True:
print(Menu())
option = int(input("Please choose an option: "))
if option == 3:
print("\nBye!")
break
if option < 1 or option > 3:
print("\nIncorrect input!")
代替按1时获得“ This”或按2时获得“ That”,它只是再次循环菜单,直到我按3退出为止。
这是我当前的输出:
Menu:
-------------
1 - Does This
2 - Does That
3 - Quit
Please choose an option: 1
Menu:
-------------
1 - Does This
2 - Does That
3 - Quit
Please choose an option: 2
Menu:
-------------
1 - Does This
2 - Does That
3 - Quit
Please choose an option: 3
Bye!
我想要的结果只是让它打印“ This”或“ That”,然后再次循环菜单直到退出。
答案 0 :(得分:4)
目前,当您选择1
或2
时,while循环将再次执行。您需要在用户输入有效值的地方实现代码。
只需澄清一下,因为您将返回Menu()
函数,所以这些行永远不会被击中,
if option == 1:
return "\nThis"
if option == 2:
return "\nThat"
编辑
这是经过改进且可以使用的版本,
input_string = """Menu:-------------
1 - Does This"
2 - Does That"
3 - Quit
Please choose an option:"""
def Menu(option):
if option == "1":
return "\nThis"
if option == "2":
return "\nThat"
while True:
option = input(input_string)
if option in {"1","2"}:
print(Menu(option))
elif option == "3":
print("\nBye!")
break
else:
print("\nIncorrect input!")
答案 1 :(得分:1)
您忘了实际编码呼叫以打印文本。试一试:
def Menu():
return("\n"
+ "Menu:\n-------------"
+ "\n1 - Does This"
+ "\n2 - Does That"
+ "\n3 - Quit"
+ "\n")
option = None
while True:
print(Menu())
option = int(input("Please choose an option: "))
if option == 3:
print("\nBye!")
break
elif option == 1:
print ("\nThis")
elif option == 2:
print ("\nThat")
elif option < 1 or option > 3:
print("\nIncorrect input!")
答案 2 :(得分:0)
您需要一些搬迁
deleteLocationItem(id) {
this.setState({
items: this.state.items.filter(item => item.id !== id)
});
}