如何在不使计数返回到0的情况下进行计数控制的循环?

时间:2019-04-06 11:02:29

标签: 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
            continue
      while count == 5:
          break

我想知道:如何在不每次都恢复为1的情况下执行此计数受控循环。我希望该程序进行一次游戏,询问用户是否要继续,然后再进行4次游戏,然后再打破并显示最终分数。任何帮助,不胜感激!

1 个答案:

答案 0 :(得分:0)

我不确定您在寻找什么,但这可能会有所帮助。

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

或者也许像这样的东西:

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")