【问题标题】:How to do a count controlled loop without the count returning back to 0?如何在计数不返回 0 的情况下进行计数控制循环?
【发布时间】:2019-04-06 11:02:29
【问题描述】:

所以我这里有这段代码:

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 次,然后显示最终分数。非常感谢任何帮助!

【问题讨论】:

  • 你完全搞乱了循环和条件语句。想想这是在做什么:while count == 5: break.
  • 你的意思是if count &lt; 5: 转换为while count &lt; 5:
  • 输入已经是字符串,不需要解析str(input[...]

标签: python count iteration increment


【解决方案1】:

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

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

【讨论】:

  • 那些continuebreak 完全没有必要。
  • 我想知道的是:在程序中循环一次,计数为 1。一旦用户输入 (1) 继续下一轮,它就会循环通过该轮。但是,在该轮结束时,它会将计数更改回 1,这永远不会停止程序。
  • @Austin 是的,你是对的。我只是按照 Oliver 的代码进行操作。
  • @Oliver 程序的第一个输入应该是什么?尤其是Do you want to continue to the next round?(1)的问题
  • 输入将是 1 或 (1) 以继续,但考虑将其更改为“是”,这样就不那么混乱了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 2021-01-29
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多