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,140 points
12 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 14,980 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,320 points
11 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
• 10,780 points
30 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,500 points
93 views
0 votes
1 answer

Error while converting a list into a numpy array

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

answered 5 days ago in Python by Rhea
10 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
55 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
• 14,980 points
3,130 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
• 14,980 points
426 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.