Skip to main content

Chapter 7a - While Loop

A while loop repeats as long as the test is true.

n = 3
while n > 0:
print(n)
n = n - 1
πŸ§ͺ Try it

Mini quiz β€” while test​

Quiz

while test

1 / 2

How many numbers are printed before done?

n = 3
while n > 0:
print(n)
n = n - 1
print("done")

Pick an answer to see if you are right.

Challenge: countdown​

Write countdown(n) so it prints n, then n-1, ... then 1 (do not print 0).

Expected for 3:

3
2
1

Mini quiz β€” while​

Quiz

while

1 / 1

When does a while loop stop?

Pick an answer to see if you are right.

Exercises​

Activity: Keep going?

Write keep_going(letter) so it returns True if the letter is y, otherwise False. (This is the question you ask at the end of a while loop.)