Python中的“while True”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3754620/
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
What does "while True" mean in Python?
提问by Baba
def play_game(word_list):
hand = deal_hand(HAND_SIZE) # random init
while True:
cmd = raw_input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
if cmd == 'n':
hand = deal_hand(HAND_SIZE)
play_hand(hand.copy(), word_list)
print
elif cmd == 'r':
play_hand(hand.copy(), word_list)
print
elif cmd == 'e':
break
else:
print "Invalid command."
While WHAT is True?
而什么是真的?
I reckon saying 'while true' is shorthand, but for what? While the variable 'hand' is being assigned a value? And what if the variable 'hand' is not being assigned a value?
我认为说 'while true' 是简写,但为了什么?当变量“hand”被赋值时?如果变量 'hand' 没有被赋值怎么办?
回答by Richard Cook
while Truemeans loop forever. The whilestatement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". Truealways evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.
while True意味着永远循环。该while语句采用一个表达式并执行循环体,而该表达式的计算结果为(布尔值)“真”。True始终评估为布尔值“true”,因此无限期地执行循环体。这是一个你最终会习惯的成语!您可能会遇到的大多数语言都有等效的习语。
Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the breakstatement in the cmd == 'e'case of the sample in your question.
请注意,大多数语言通常都有一些机制可以提前跳出循环。在 Python 的情况下,它是您问题中示例的break语句cmd == 'e'。
回答by Chris Dodd
while Trueis true -- ie always. This is an infinite loop
whileTrue是真的——即总是。这是一个无限循环
Note the important distinction here between Truewhich is a keyword in the language denoting a constant value of a particular type, and 'true' which is a mathematical concept.
请注意这里的重要区别,Truewhich 是语言中的关键字,表示特定类型的常量值,而 'true' 是一个数学概念。
回答by H?vard S
Trueis always True, so while Truewill loop forever.
Trueis always True,所以while True将永远循环。
The whilekeyword takes an expression, and loops while the expression is true. Trueis an expression that is always true.
所述while关键字接受一个表达,并循环而表达式为真。True是一个永远为真的表达。
As a possibly clarifying example, consider the following:
作为一个可能的澄清示例,请考虑以下内容:
a = 1
result = a == 1
Here, a == 1will return True, and hence put Trueinto result. Hence,
在这里,a == 1将返回True,因此把True成result。因此,
a = 1
while a == 1:
...
is equivalent to:
相当于:
while True:
...
provided you don't alter the value of ainside the whileloop.
只要你不改变循环a内的值while。
回答by Platinum Azure
A whileloop takes a conditional argument (meaning something that is generally either true or false, or can be interpreted as such), and only executes while the condition yields True.
甲while环路采用条件参数(意味着东西,通常是真或假,或可以解释为这样),以及只执行而条件的产率True。
As for while True? Well, the simplest true conditional is Trueitself! So this is an infinite loop, usually good in a game that requires lots of looping. (More common from my perspective, though, is to set some sort of "done" variable to false and then making that true to end the game, and the loop would look more like while not done:or whatever.)
至于while True?好吧,最简单的真条件就是True它自己!所以这是一个无限循环,通常适用于需要大量循环的游戏。(不过,从我的角度来看,更常见的是将某种“完成”变量设置为 false,然后将其设为 true 以结束游戏,循环看起来更像while not done:或其他什么。)
回答by Daniel Vandersluis
whileloops continue to loop until the condition is false. For instance (pseudocode):
while循环继续循环直到条件为假。例如(伪代码):
i = 0
while i < 10
i++
With each iteration of the loop, iwill be incremented by 1, until it is 10. At that point, the condition i < 10is no longer true, and the loop will complete.
随着循环的每次迭代,i将递增 1,直到它为 10。此时,条件i < 10不再为真,循环将完成。
Since the condition in while Trueis explicitly and always true, the loop will never end (until it is broken out of some other way, usually by a construct like breakwithin the loop body).
由于条件 inwhile True是显式且始终为true,循环将永远不会结束(直到它以其他方式中断,通常是通过像break在循环体内的构造)。
回答by Copas
In some languages True is just and alias for the number. You can learn more why this is by reading more about boolean logic.
在某些语言中,True 只是数字的别名。您可以通过阅读有关布尔逻辑的更多信息来了解更多原因。
回答by Dagg Nabbit
my question: while WHAT is True?
我的问题:什么是真的?
While Trueis True.
虽然True是True。
The while loop will run as long as the conditional expression evaluates to True.
只要条件表达式的计算结果为 ,while 循环就会运行True。
Since Truealways evaluates to True, the loop will run indefinitely, until something within the loop returns or breaks.
由于True始终评估为True,循环将无限期运行,直到循环returns 或breaks 中的某些内容。
回答by JD Isaacks
my question: while WHAT is True?
我的问题:什么是真的?
Everything inside the () of the while statement is going to be evaluated as a boolean. Meaning it gets converted into either true or false.
while 语句的 () 内的所有内容都将被评估为布尔值。这意味着它被转换为真或假。
Consider in the statement while(6 > 5)
在声明中考虑 while(6 > 5)
It first evaluates the expression 6 > 5which is trueso is the same as saying while(true)
它首先评估表达式6 > 5,这true与说的一样while(true)
Anything that is not FALSE, 0, an emptry string "", null, or undefined is likely to be evaluated to true.
任何不是 FALSE、0、空字符串 ""、null 或未定义的东西都可能被评估为真。
When I first started programming I used to do things like if(foo == true), I didn't realise that was virtually the same thing as if(foo).
当我第一次开始编程时,我曾经做过类似的事情if(foo == true),我没有意识到这与if(foo).
So when you say while(true)its like are saying while(true == true)
所以当你说它while(true)就像在说while(true == true)
So to answer you question: While TRUE is True.
所以回答你的问题:虽然 TRUE 是真的。
回答by dheerosaur
Nothing evaluates to Truefaster than True. So, it is good if you use while Trueinstead of while 1==1etc.
没有什么True比True. 因此,如果您使用while True代替while 1==1等,则很好。
回答by Paul Butcher
In this context, I suppose it could be interpreted as
在这种情况下,我想它可以被解释为
do
...
while cmd != 'e'

