【发布时间】:2021-08-20 15:32:34
【问题描述】:
我目前正在为我的评分系统制作一个主菜单,通过使用 while 循环作为一种简单的验证形式,将用户带到代码的不同部分,但它会导致一个永无止境的 while 循环,因为我的一些定义也有while 循环也是如此。这就是我所做的
def menu():
print("""\n Main Menu \n
1) Join the Event as a Team
2) Join the Event by Yourself
3) Score Screen""")
menuchoice_option = ["1","2","3"]
menuchoice = ""
while menuchoice not in menuchoice_option:
menuchoice = input("Please enter your choice here:")
if menuchoice == "1":
team_menu()
menuchoice = True
elif menuchoice == "2":
individual()
menuchoice = True
elif menuchoice == "3":
#scorescreen()
menuchoice = True
else:
menuchoice = False
print("Please enter a value between 1-3")
menuchoice = input("Please enter your choice here:")
这是菜单 def 导致执行无限循环的其他功能
def team_menu():
global teamS
team_player = ""
while team_player != "":
team_player = input("What is your name:")
print("""\n Available Teams \n
1) Team Ahab
2) Team Ishmael
\n 3) Go back to Main Menu\n""")
team_choice = ""
team_choice_option = ["1","2","3"] # all valid choices on team menu
while team_choice not in team_choice_option:
team_choice = input("Enter your choice here:")
if team_choice == "1":
teamS["Team 1"]["Team Ahab"].append(team_player)
print(teamS["Team 1"])
print("Thank You for Joining Team Ahab")
team_choice = True
elif team_choice == "2":
teamS["Team "+ team_choice]["Teeam Ishmael"].append(team_player)
print(teamS["Team 2"])
print("\nThank You for Joining Team Miller\n")
team_choice = False
elif team_choice == "3":
menu()
team_choice = True
else:
print("Enter a value between 1-3")
team_choice = False
我的理想输出是它停止在我的代码中导致来自不同定义的无限 while 循环。我是初学者,请执行我
【问题讨论】:
-
我们还必须查看您的导致无限循环提供帮助的函数
-
你的
menuchoice是用户输入的字符串;然后你用menuchoice = True覆盖它。不知道这背后的想法是什么。 -
如果你在
team_menu或individual中再次调用menu(),你就做错了。 -
您是在 Python 3 还是 Python 2 解释器中运行代码?如果您在输入提示符下键入
"1"(带引号)会发生什么?input()函数在 Python 2 中的工作方式非常不同。我只能使用 Python 2 解释器重现您的问题。 -
对布尔值使用不同的变量
标签: python function while-loop boolean