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?

Aug 8, 2018 in Python by Priyaj
• 56,520 points
19 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 Aug 8, 2018 by bug_seeker
• 15,350 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 Jun 4, 2018 in Python by ariaholic
• 7,340 points
19 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
55 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
126 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
30 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 221 views
+4 votes
6 answers
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,350 points
5,585 views
0 votes
1 answer

how to exit a python script in an if statement

This works fine for me: while True: ...READ MORE

answered Sep 19, 2018 in Python by bug_seeker
• 15,350 points
2,162 views