Chapters
Python114 chapters

Exercises

While Loops

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Print 0, 1 and 2 with a while loop.

Python
count = 0
# loop while count is under 3
Exercise 2

How many years at 10% growth does it take for 100 to pass 150? Print the number of years.

Python
balance = 100
years = 0
# grow until balance is at least 150
print(years)
Exercise 3

This loop never ends. Fix it so it prints 1, 2 and 4 and stops.

Python
n = 0
while n < 5:
    if n == 3:
        continue
    n += 1
    print(n)