Python 如何在计数不返回0的情况下执行计数控制循环?
我这里有一段代码:Python 如何在计数不返回0的情况下执行计数控制循环?,python,count,iteration,increment,Python,Count,Iteration,Increment,我这里有一段代码: count = 1 if count < 5: print ("If 5 rounds have passed, enter any other key: ") nextr = str(input("Do you want to continue to the next round?(1)" + "\n")) if nextr in ("1", "(1)"): count += 1 co
count = 1
if count < 5:
print ("If 5 rounds have passed, enter any other key: ")
nextr = str(input("Do you want to continue to the next round?(1)" + "\n"))
if nextr in ("1", "(1)"):
count += 1
continue
while count == 5:
break
count=1
如果计数小于5:
打印(“如果已通过5轮,请输入任何其他键:”)
nextr=str(输入(“您想继续下一轮吗?(1)”+“\n”))
如果下一个输入(“1”,即“1”):
计数+=1
持续
当计数=5时:
打破
我想知道:我怎么能做这个计数控制循环,而不让它每次都回复到1。我想让程序进行一次游戏,询问用户是否想继续,然后在打破之前再进行4次,然后显示最终分数。非常感谢您的帮助 我不知道你在找什么,但这可能会有帮助
print ("If 5 rounds have passed, enter any other key")
count = 1
while count <= 5:
nextr = (input("Do you want to continue to the next round?(Yes/No)"))
print ("count = ",count)
if nextr.lower() in ("y", "yes"):
count += 1
print(“如果已通过5轮,请输入任何其他键”)
计数=1
当计数时,循环和条件语句会把你弄得一团糟。想想这是怎么做的:而count==5:break
。如果count<5:
转换为而count<5:
?输入已经是一个字符串,您不需要解析str(输入[…]
那些continue
和break
是完全不必要的。我想知道的是:在程序中循环一次,计数为1。一旦用户输入(1)要继续下一轮,它将循环该轮。然而,在该轮结束时,它将计数改回1,这不会停止程序。@Austin是的,你是对的。我只是跟着Oliver的代码。@Oliver你的程序的第一个输入应该是什么?特别是关于的问题,你想继续到n吗下一轮?(1)
输入将是1或(1)以继续,但考虑将其更改为“是”,因此不太容易混淆。
import time
def round_5():
count = 1
while count <= 5:
print ("Count = ",count)
count += 1
time.sleep(1)
nextr = "y"
print ("If 5 rounds have passed, enter any other key:")
while nextr.lower() in ("yes","y"):
round_5()
nextr = (input("Do you want to continue to the next round?(Yes/No)"))
print ("Thank you, have a nice day")