5.4: 🔁 While Loops

Topic

5.4: 🔁 While Loops

Topic Progress:

While loops work in a pretty similar way to if statements, but they run over and over again until the condition is false. Note: This makes it easy to accidentally write an “infinite loop”, a loop that never stops.

Below is an example of a while loop that runs three times:

x = 3
while(x > 0):
 x = x - 1
print("This code runs three times")

So with each cycle of the loop, x changes from 3 -> 2 -> 1 -> 0. Once x is 0, the condition that x is greater than 0 is no longer true, so the loop stops running.

For this lesson, we are developing a state-of-the-art counting algorithm that prints out the numbers from 1-10. There is a simple bug somewhere in the code below. Your task is to fix it.

@

Not recently active