break命令后如何重新启动while循环?

时间:2019-04-22 01:00:39

标签: python-3.x

while True:
    restart=False
    userguess=int(input("guess"))
    if userguess not in range(0,100):
          restart=True
          break     
    if userguess>computerguess:
          print("guessed higher")
    elif userguess<computerguess:
          print("guessed lower")
    else:
          print("congrats, userguess equals computerguess")
          break

/ *如果猜测数字超出0到100范围,我想循环中断。有用。但是在那之后,我想再次询问用户输入,如果下一个值在范围内,我希望循环重新启动。请帮助* /

1 个答案:

答案 0 :(得分:0)

使用继续而不是中断

while True:
    restart=False
    userguess=int(input("guess"))
    if userguess not in range(0,100):
          restart=True
          break     
    if userguess>computerguess:
          print("guessed higher")
    elif userguess<computerguess:
          print("guessed lower")
    else:
          print("congrats, userguess equals computerguess")
          continue