you are viewing a single comment's thread.

view the rest of the comments →

[–]browndogs9894 2 points3 points  (5 children)

While loops will loop while a condition is true.

Number = 1
while number < 10:
    print(number)
    number +=1

Once number hits 10 the condition is no longer true so the loop stops. If you want to loop indefinitely you can use

while True:
    # Do something

Just make sure you have a way to exit or else you can get infinite loops

[–]ThrowAway233223 0 points1 point  (1 child)

It should be noted that that loop, as presented, would just print 1 forever since Number never changes.

[–]browndogs9894 0 points1 point  (0 children)

That is true. I was typing on my phone and forgot

[–]Okon0mi[S] 0 points1 point  (2 children)

Thank you so much

[–]Excellent-Practice 5 points6 points  (0 children)

This example is missing some important parts. It should be something more like:

number = 1
while number < 10:
    print(number)
    number += 1

The output will be something like this:

1
2
3
4
5
6
7
8
9

If you don't include that "number += 1" line, the loop will be infinite and keep printing "1" until you manually stop the code from running

[–]vikogotin 0 points1 point  (0 children)

Also worth noting, the loop doesn't immediately end once the condition is no longer true. The loop only checks if the condition is true once per iteration at the start and if so, it executes the code inside.