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
• 58,100 points
80 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,550 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 aryya
• 7,440 points
110 views
0 votes
1 answer

.What is the meaning of "while True :" while doing in a loop.

The body of the loop will continue ...READ MORE

answered Oct 7, 2020 in Python by Gitika
• 65,870 points
47 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,250 points
488 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
798 views
0 votes
2 answers
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,864 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,436 views
+2 votes
4 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,550 points
33,768 views
0 votes
3 answers

how to exit a python script in an if statement

In Python, the break statement provides you with the opportunity ...READ MORE

answered Dec 13, 2020 in Python by Roshni
• 10,420 points
49,184 views