PYTHON CORE · LESSON 16

while loop

Repeat while you're right. · about 13 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Check before each step

while repeats the action as long as the condition is correct. When it goes wrong, the loop stops.

count = 1
while count < 4:
    print(count)
    count = count + 1
The number changes on each circle.
Step 11Step 22Step 33Stop.4 < 4 — нет

✦ The screen will be 1, 2, 3.

STEP 2

Don't forget to change the number

If you do not change count inside the loop, it can be repeated indefinitely. The last line adds one.

count = 1
while count < 3:
    count = count + 1
print(count)

✦ After two steps, count becomes 3.

IN-CLASS PRACTICE

Type the code yourself

Reprint the loop. Both lines within it must have an indentation.

step = 1
while step < 4:
    print(step)
    step = step + 1
Open the editor for class work
HOMEWORK

Solve it yourself

With while, output the numbers 1, 2, 3. Start with n = 1 and increase n per unit within the loop.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework