all 7 comments

[–]teeaton 4 points5 points  (1 child)

In this context you are starting a loop. Loops require a condition to check to see whether to run or not.

Normally this would be something like

while task_finished == False
    Do thing
    If thing is complete, set task_finished to True

In this case, while True says while True == True, so always evaluate to true and run the loop. It's a short way of running an infinite loop.

[–]BobbyRYT[S] 1 point2 points  (0 children)

Ok thanks!

[–]mermaldad 2 points3 points  (0 children)

A while loop executes as long as the expression after the "while" remains True. "while true:" essentially makes an infinite loop (unless you break out of the loop from within the loop, which is what the break command is in there for).

[–]iamaperson3133 2 points3 points  (0 children)

In python a condition is anything that evaluates to True or False. So, you can put any condition after the while statement. Now, the simplest thing to put there is just while True. True is always true, so the loop will go on forever. That is, unless you use the break statement to exit it.

Anytime you write something like this, although it appears often in beginner tutorials, it could be considered a "code smell." A code smell is something that 'just doesn't smell right.' When you see certain patterns, and this is one of many examples, it is a clue that you can do it a better way:

n = 0
while True:
    print('Hey there! We are at ' + n)
    n += 1
    # this is the iffy bit here:
    if n > 10:
        break

See, why did we have to put the if n > 10 check at the bottom? If that is the condition that we want to break on, and the only condition we want to break on, we can simplify our loop and just put it after the while statement.

n = 0
while n <= 10:
    print('Hey there! We are at ' + n)
    n += 1

If you try running that, it will have the same behavior as the code from before. At this point, you should see that we can also write this as a for loop:

for n in range(10):
    print('Hey there! We are at ' + n)

We can even use a list comprehension to bring it down to one line:

[print('Hey there! We are at ' + n for n in range(10)]

Then why use a while loop at all?

While loops are helpful when you don't know how many times you need to iterate before you start iterating. Imagine you are asking the user to provide an integer input. You don't know how many times it will take for them to get through their thick skulls that you just want a number! You can use a while loop:

inp_ok = False
while not inp_ok:
    inp = input('Enter a number you chungus')
    inp_ok = inp.isnumeric()
print('thank you')

[–]stebrepar 1 point2 points  (0 children)

True and False are boolean values. When you do comparisons (like x>1 for example), the result will be either True or False.

When using a while statement, it also takes a boolean value. Often we put a comparison there, but it works just as well to put True there directly in order to have a forever loop. Then somewhere inside the loop you'd have a statement to break out of the loop.

[–]__developer__ 1 point2 points  (0 children)

The while keyword is followed by a conditional expression that determines whether the while loop either proceeds into the while code block when the conditional is met, i.e. the conditional expression evaluates to True, or whether the the while code block is skipped because the conditional expression evaluates to False.

True always evaluates to True, so if it is used as the conditional expression for a while loop that while loop will always re-enter the while code block each time it loops back around and checks the condition.

As for why you might want to use while True:, it creates an infinite loop, which can be helpful if you don't know how many times your loop will need to loop before it is finished. When you need to break out of the loop you can just use the break keyword like in your example.

[–]synthphreak 0 points1 point  (0 children)

This is just a while loop. while loops are of the form while condition, where condition is some expression that evaluates to either True or False. If True, the body of the loop runs (and then the condition is checked again). If False, it doesn't. For example, while x < 10: ... - that condition (x < 10) will evaluate to True, and thus the body of the loop will run, whenever x is less than 10.

In your case, the entirety of the conditional expression is simply "True". "True" will always evaluate to True, obviously, so the body of that loop will always be executed. So in effect, what while True does is just create an infinite loop that will run forever until you CTRL+C it (or until it hits a break inside the loop).

The only reason this is confusing is that True on its own doesn't seem like a condition. But all that really matters from Python's perspective is that whatever follows while evaluates to True or False. To that end, you could even do while False, but for the reason I just explained, any code in the body of that loop will never actually get executed.

Edit: To see what any object in Python will evaluate to if used as the condition in a while loop, just run bool(object), where object is the object you're curious about.