【发布时间】:2020-05-21 03:26:38
【问题描述】:
我尝试过在 SO 上进行谷歌搜索和搜索,但我无法弄清楚为什么我在倒数第二行的中断没有退出 while 循环。更好的是,我无法弄清楚为什么循环也没有继续。我的目的是让用户有可能在最后一个选择之后前往主菜单(基本上是 menuchoice 的 while 循环(这是我在此处粘贴的一个循环)。
有什么建议吗?先感谢您。感觉好像我错过了一些重要的东西。
#this is going to show how many exercise weapons you need for next magic level
if menuchoice == "4":
#these functions returns the amount of magic wands/rods that is needed to be spent for next magic level
print("Select vocation")
print("Press P for Royal Paladin")
#ask user to input vocation:
while True:
vocationchoice = input()
if vocationchoice == "P" or vocationchoice == "p":
#ask user to input magic level for paladin
num1 = float (input("Enter your magic level: "))
#ask for own training dummy
print("Do you have your own exercise dummy? Type Y for yes and N for no.")
while True:
trainingdummy = input()
if trainingdummy == "y" or trainingdummy == "Y":
#list the different exercise weapons
print("Select exercise weapon:")
print("1. Training rod")
#loop, where we ask user to input what exercise weapon they want to calculate
while True:
while True:
weaponchoice = input()
if weaponchoice == "q":
sys.exit() #quit the program
if weaponchoice == "1" or weaponchoice == "2" or weaponchoice == "3" or weaponchoice == "f":
break #break out of the input loop
#User choice
if weaponchoice == "1":
print("The amount of training rods needed for next magic level is " + str((nextmaglvlpalwithdummy(num1))) + ".")
if trainingdummy == "n" or trainingdummy == "N":
#list the different exercise weapons
print("Select exercise weapon:")
print("1. Training rod")
#loop where ask user to input what exercise weapon they want to calculate
while True:
weaponchoice = input()
#User choice
if weaponchoice == "1":
print("The amount of training rods needed for next magic level is " + str((nextmaglvlpal(num1))) + ".")
elif weaponchoice == "f":
break
print("\nGo to main menu? Press F.")
【问题讨论】:
-
你说的是第一个break语句还是最后的第二个?
-
也许,您可以使用布尔值
variable和var = False代替break,而不是break -
@LouisLac 最后一个。它什么也没做。如果我删除那个中断,我是否应该能够再次按“1”并获得相同的结果?如果它在那个循环内?
-
预期的行为是当按下“f”返回主菜单时的break语句,例如退出所有之前的while循环?现在,当按下“f”时,您的代码将留在训练虚拟“选择循环中。
-
这是预期的行为,是的。但同样,如果我删除最后一行的 break 语句,我应该能够再次按“1”,它应该会给我结果。第一次按“1”后,它会停留在那里,就像它不是 while 循环的一部分一样。很奇怪。
标签: python while-loop break