a basic question about “while true”

0 votes

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?

Jun 4, 2018 in Python by charlie_brown
• 7,720 points
20 views

1 answer to this question.

0 votes
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.
answered Jun 4, 2018 by ariaholic
• 7,340 points

Related Questions In Python

0 votes
1 answer

A basic question about “while true”

while True means loop forever. The while ...READ MORE

answered Aug 8, 2018 in Python by bug_seeker
• 15,350 points
21 views
0 votes
1 answer

Return a list inside a for loop while iterating over the elements of another list

The print() is getting called multiple times ...READ MORE

answered Sep 21, 2018 in Python by SDeb
• 13,160 points
58 views
0 votes
1 answer

Question on PyQt: How to connect a signal to a slot to start a background operation in Python

It shouldn't matter whether the connection is ...READ MORE

answered Nov 27, 2018 in Python by Nymeria
• 3,520 points
147 views
0 votes
1 answer

Error while converting a list into a numpy array

The only possible reason I could think ...READ MORE

answered Jun 13 in Python by Rhea
42 views
0 votes
0 answers
0 votes
1 answer
0 votes
0 answers
0 votes
0 answers

Error while saving a python file

Following error when I try to save ...READ MORE

Aug 5 in Python by Ashish
11 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
98 views
0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
41 views