如何跳出 Python 中的 while 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14594522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:56:17  来源:igfitidea点击:

How to break out of while loop in Python?

pythonwhile-loopbreak

提问by Ninja

I have to make this game for my comp class, and I can't figure out how how break out of this loop. See, I have to play against the "computer," by rolling bigger numbers, and seeing who has the bigger score. But I can't figure out how to "break" from my turn, and transition to the computers turn. I need "Q" (quit) to signal the beginning of the computers turn, but I don't know how to do it.

我必须为我的比赛班制作这个游戏,我不知道如何打破这个循环。看,我必须与“计算机”对战,滚动更大的数字,看看谁的分数更高。但我不知道如何从我的回合“中断”,并过渡到计算机回合。我需要“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

回答by aug2uag

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

回答by Phil

What I would do is run the loop until the ans is Q

我会做的是运行循环直到 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

回答by John La Rooy

A couple of changes mean that only an Ror rwill roll. Any other character will quit

一些变化意味着只有一个Rorr会滚动。任何其他角色都会退出

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

回答by Steve Todd

Don't use while True and break statements. It's bad programming.

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

Imagine you come to debug someone else's code and you see a while True on line 1 and then have to trawl your way through another 200 lines of code with 15 break statements in it, having to read umpteen lines of code for each one to work out what actually causes it to get to the break. You'd want to kill them...a lot.

想象一下,你来调试别人的代码,你在第 1 行看到一个 while 为 True,然后你必须在另外 200 行代码中搜索,其中包含 15 个 break 语句,必须阅读无数行代码才能计算出来实际上是什么导致它休息。你想杀了他们......很多。

The condition that causes a while loop to stop iterating should always be clear from the while loop line of code itself without having to look elsewhere.

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

Phil has the "correct" solution, as it has a clear end condition right there in the while loop statement itself.

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