【问题标题】:How to break out of while loop in Python?如何在 Python 中跳出 while 循环?
【发布时间】:2013-01-13 17:01:10
【问题描述】:

我必须为我的比赛课程制作这个游戏,但我不知道如何打破这个循环。看,我必须通过滚动更大的数字来对抗“计算机”,看看谁的分数更大。但我不知道如何从轮到我“打破”,并过渡到计算机轮到。我需要“Q”(退出)来表示计算机开始转动,但我不知道该怎么做。

ans=(R)
while True:
    print('Your score is so far '+str(myScore)+'.')
    print("Would you like to roll or quit?")
    ans=input("Roll...")
    if ans=='R':
        R=random.randint(1, 8)
        print("You rolled a "+str(R)+".")
        myScore=R+myScore
    if ans=='Q':
        print("Now I'll see if I can break your score...")
        break

【问题讨论】:

  • 按照你的方式使用break 很好,但你必须准确输入Q。例如,q 将不起作用。第一行应该是ans=('R') 吗?反正你不需要它

标签: python while-loop break


【解决方案1】:
ans=(R)
while True:
    print('Your score is so far '+str(myScore)+'.')
    print("Would you like to roll or quit?")
    ans=input("Roll...")
    if ans=='R':
        R=random.randint(1, 8)
        print("You rolled a "+str(R)+".")
        myScore=R+myScore
    else:
        print("Now I'll see if I can break your score...")
        ans = False
        break

【讨论】:

    【解决方案2】:

    我要做的是运行循环,直到 ans 为 Q

    ans=(R)
    while not ans=='Q':
        print('Your score is so far '+str(myScore)+'.')
        print("Would you like to roll or quit?")
        ans=input("Roll...")
        if ans=='R':
            R=random.randint(1, 8)
            print("You rolled a "+str(R)+".")
            myScore=R+myScore
    

    【讨论】:

      【解决方案3】:

      一些更改意味着只有Rr 会滚动。任何其他角色都会退出

      import random
      
      while True:
          print('Your score so far is {}.'.format(myScore))
          print("Would you like to roll or quit?")
          ans = input("Roll...")
          if ans.lower() == 'r':
              R = np.random.randint(1, 8)
              print("You rolled a {}.".format(R))
              myScore = R + myScore
          else:
              print("Now I'll see if I can break your score...")
              break
      

      【讨论】:

      • 如果需要请纠正我 - break 发送一个 False 信号来停止 while 循环?
      • @SIslam 有点像。 break 停止 while 循环,但没有“错误信号”:while 表示“循环,而 while 语句后面的表达式计算为 True”,所以如果 while 之后的内容是True 本身,while 将永远循环; break 表示“立即停止循环”,可以在任何循环中使用,包括 whilefor 循环。
      【解决方案4】:

      不要使用 while True 和 break 语句。这是糟糕的编程。

      想象一下,你来调试别人的代码,你在第一行看到了一段时间 True,然后不得不在另外 200 行代码中搜索,其中包含 15 个 break 语句,必须为每个语句读取无数行代码找出真正导致它中断的原因。你会想杀了他们……很多。

      导致 while 循环停止迭代的条件应始终从 while 循环代码行本身中清楚,而无需查看其他地方。

      Phil 有一个“正确”的解决方案,因为它在 while 循环语句本身中有一个明确的结束条件。

      【讨论】:

        【解决方案5】:

        Walrus operator(python 3.8 中添加的赋值表达式)和while-loop-else-clause 可以做得更pythonic:

        myScore = 0
        while ans := input("Roll...").lower() == "r":
            # ... do something
        else:
            print("Now I'll see if I can break your score...")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-04
          • 1970-01-01
          • 2022-01-06
          相关资源
          最近更新 更多