tags:

views:

880

answers:

14

level: beginner

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

my question: 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?

+33  A: 

while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always 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.

Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.

Richard Cook
For clarity sake I believe you can do while(true == true) in most languages. This may help for a new programmer to better understand the logic behind it.
Chris
Chris, `while True==True` is ugly. It is as ugly as `while 'apples'!='oranges'` :)
dheerosaur
@Chris: I can't see how adding syntax will help.
S.Lott
As one of my teachers pointed out: if you think `true==true` is an improvement, where do you stop? Wouldn't `(true==true)==true` be even better?
Ken
It's only a clarification for those that don't understand you don't have to have a comparison there, only a boolean. (true==true) isn't better in a general sense, only to explain something. It's not something you'd do it the real world, or any other time.
Malfist
I think I've seen this around somewhere. `bool ever = true; for (;ever;) { }`
ChaosPandion
If you think `while True == True` is good and `while False == False` is bad, you have misunderstood everything.
Johannes Schaub - litb
@Chris, eventually one should be aware that a boolean value or variable can be used in place of a logic expression, and "eventually" should be as soon as possible.
Mark Ransom
@Mark eventually one should be aware that at times it takes a bit of additional help to get a new programmer to understand boolean logic and the like. All I was trying to do was help him understand it.
Chris
+5  A: 

while True is true -- ie always. This is an infinite loop

Note the important distinction here between True which is a keyword in the language denoting a constant value of a particular type, and 'true' which is a mathematical concept.

Chris Dodd
Technically speaking, you're wrong. The construct means "while true is not false". You can see the distinction if you write `while(5)` in C, where there are no proper booleans. It doesn't mean `while(5==1)`, it means `while(5!=0)`.
Blindy
I think it's more appropriate to say 'indefinite loop;' the assumption must be that the loop will be interrupted by a break or return at some point. Truly 'infinite' loops are programmer error; 'indefinite loops' are created by design.
no
@Blindy: in a weakly-typed language with automatic casting, you can say x 'evaluates to' y... it's not while 5 is not strictly equal to 0, it's while 5 is loosely equal to True.
no
@Blindy, `while(5)` in C means while `5` is true, not while its `true` or `0`, which is something completely different. `5` is always true. In general, when languages have a keyword `true`, its a constant that is true, but not the only constant that is true. Similarly `false` is not necessarily the only value that is false.
Chris Dodd
A: 

True is always True, so while True will loop forever.

The while keyword takes an expression, and loops while the expression is true. True is an expression that is always true.

As a possibly clarifying example, consider the following:

a = 1
result = a == 1

Here, a == 1 will return True, and hence put True into result. Hence,

a = 1
while a == 1:
  ...

is equivalent to:

while True:
  ...

provided you don't alter the value of a inside the while loop.

Håvard S
`True` isn't *always* `True`, e.g. `True = False`. ;-)
jathanism
@jathanism- Truth is truth, To the end of reckoning.
dheerosaur
+1  A: 

A while loop 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.

As for while True? Well, the simplest true conditional is True itself! 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.)

Platinum Azure
+1  A: 

while loops continue to loop until the condition is false. For instance (pseudocode):

i = 0
while i < 10
  i++

With each iteration of the loop, i will be incremented by 1, until it is 10. At that point, the condition i < 10 is no longer true, and the loop will complete.

Since the condition in while True is explicitly and always true, the loop will never end (until it is broken out of some other way, usually by a construct like break within the loop body).

Daniel Vandersluis
+1  A: 

In some languages True is just and alias for the number. You can learn more why this is by reading more about boolean logic.

Copas
+11  A: 

my question: while WHAT is True?

While True is True.

The while loop will run as long as the conditional expression evaluates to True.

Since True always evaluates to True, the loop will run indefinitely, until something within the loop returns or breaks.

no
This is python. There is no parenthesized expression ;P
Mike Axiak
heh, good point. I'm not used to thinking in Python. Although I suppose you _could_ put parens around it if you wanted...
no
Python has different syntax
vol7ron
+3  A: 

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.

Consider in the statement while(6 > 5)

It first evaluates the expression 6 > 5 which is true so is the same as saying while(true)

Anything that is not FALSE, 0, an emptry string "", null, or undefined is likely to be evaluated to true.

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

So when you say while(true) its like are saying while(true == true)

So to answer you question: While TRUE is True.

John Isaacks
A: 

Nothing evaluates to True faster than True. So, it is good if you use while True instead of while 1==1 etc.

dheerosaur
Funniest thing I saw today!
Blindy
@downvoter - May I know why?
dheerosaur
+2  A: 

In this context, I suppose it could be interpreted as

do
...
while cmd  != 'e' 
Paul Butcher
+1: a syntactic reinterpretation of the loop
drewk
+1  A: 

Formally, True is a Python built-in constant of bool type.

You can use Boolean operations on bool types (at the interactive python prompt for example) and convert numbers into bool types:

>>> print not True
False
>>> print not False
True
>>> print True or False
True
>>> print True and False
False
>>> a=bool(9)
>>> print a
True
>>> b=bool(0)
>>> print b
False
>>> b=bool(0.000000000000000000000000000000000001)
>>> print b
True

And there are "gotcha's" potentially with what you see and what the Python compiler sees:

>>> n=0
>>> print bool(n)
False
>>> n='0'
>>> print bool(n)
True
>>> n=0.0
>>> print bool(n)
False
>>> n="0.0"
>>> print bool(n)
True

As a hint of how Python stores bool types internally, you can cast bool types to integers and True will come out to be 1 and False 0:

>>> print True+0
1
>>> print True+1
2
>>> print False+0
0
>>> print False+1
1

But you should not rely on that! It is just interesting. In many C programs, which lacks a bool type, there are commonly these definitions in standard header files to define TRUE and FALSE to be used logically in the C program:

#ifndef FALSE
#define FALSE (0)
#define TRUE (!(FALSE))
#endif

or

#ifndef (TRUE)
#define TRUE (1)
#endif

This is where the True==1 assumption came from I suppose. Python does have a bool type and it would be poor practice to assume any value for True other than True.

The more important part of your question is "What is while True?" And an important corollary: What is false?

First, for every language you are learning, learn what the language considers TRUE and FALSE. Python considers Truth slightly differently than Perl Truth for example. Other languages have slightly different concepts of true / false. Know what your language considers to be True and False for different operations and flow control to avoid many headaches later!

There are many algorithms where you want to process something until you find what you are looking for. Hence the infinite loop or indefinite loop. Each language tend to have its own idiom for these constructs. Here are common C infinite loops, which also work for Perl:

for(;;) { /* loop until break */ }
while (1) {
   return if (function(arg) > 3);
}

The while True: form is common in Python for indefinite loops with some way of breaking out of the loop. Learn Python flow control to understand how you break out of while True loops. Unlike most languages, for example, Python can have an else clause on a loop. There is an example in the last link.

drewk
@downvoter: why?
drewk
A: 
while True:
    ...

means infinite loop.

The while statement is often used of a finite loop. But using the constant 'True' guarantees the repetition of the while statement without the need to control the loop (setting a boolean value inside the iteration for example), unless you want to break it.

In fact

True == (1 == 1)
Roberto
A: 

while True mean infinite loop, this usually use by long process. you can change

while True:

with

while 1:
Gunslinger_
A: 

To answer your question directly: while the loop condition is True. Which it always is, in this particular bit of code.

Marius Gedminas