我正在尝试一个循环gooing,但后来我让用户输入5退出循环。我在python 3
def main():
print("Welcome to the List Info Checker")
printMenu()
printValue = input("Please enter a number between 1 and 5(inclusive): ")
while printValue != 5:
if printValue == 1:
print("1")
elif printValue == 2:
# allTheSame()
print("2")
elif printValue == 3:
# allDifferent()
print("3")
elif printValue == 4:
# sortThis()
print("4")
main()
答案 0 :(得分:2)
有两种方法可以打破while
循环
让语句以某种方式发生变化,使其不再是True
使用break
命令突破最低级别循环'
因为只在循环之前询问用户输入,所以在循环开始后无法改变,导致无限循环。如果输入在循环内部,则可以通过用户输入5来打破它,因为每次循环重新启动时都会询问:
printValue = input("Please enter a number between 1 and 5(inclusive): ")
while printValue != 5:
if printValue == 1:
print("1")
elif printValue == 2:
# allTheSame()
print("2")
elif printValue == 3:
# allDifferent()
print("3")
elif printValue == 4:
# sortThis()
print("4")
printValue = input("Please enter a number between 1 and 5(inclusive): ")
答案 1 :(得分:0)
您需要在循环中提示输入,否则它始终只是检查第一次输入的相同值。试试这个:
def main():
print("Welcome to the List Info Checker")
printMenu()
printValue = input("Please enter a number between 1 and 5(inclusive): ")
while printValue != 5:
if printValue == 1:
print("1")
elif printValue == 2:
# allTheSame()
print("2")
elif printValue == 3:
# allDifferent()
print("3")
elif printValue == 4:
# sortThis()
print("4")
printValue = input("Please enter a number between 1 and 5(inclusive): ")
main()
答案 2 :(得分:0)
你可以使用“break”
print("Welcome to the List Info Checker")
printMenu()
while True:
if printValue == 1:
print("1")
elif printValue == 2:
# allTheSame()
print("2")
elif printValue == 3:
# allDifferent()
print("3")
elif printValue == 4:
# sortThis()
print("4")
elif printValue == 5:
break
或最后两行
else:
break