If the condition never becomes false, the loop runs forever and your program freezes. Always make sure something changes inside the loop (like count = count - 1) so it can eventually stop.
๐ break and continue
break โ jump out of the loop immediately.
continue โ skip to the next time around.
for i in range(1, 10):
if i == 5:
break # stop the loop at 5
print(i)
# prints 1 2 3 4
โ What you learned
for loops repeat a set number of times, often with range().
for can step through strings and lists.
while loops repeat as long as a condition is true.
break stops a loop; continue skips to the next round.
๐ฎ Time to Practice!
Let the computer do the boring repetition for you. ๐
๐ข
Task 1: Count to Five
Easy
Use a for loop with range() to print the numbers 1 through 5, each on its own line.
for i in range(1, 6):
print(i)
๐ฃ
Task 2: Cheer Squad
Easy
Print Go Team! (with a trailing space) 3 times on one line. Hint: print("Go Team! ", end="") keeps printing on the same line.
for i in range(3):
print("Go Team! ", end="")
โก
Task 3: Even Numbers
Medium
Print the even numbers from 2 to 10. Two ways to try: use range(2, 11, 2), or loop 1โ10 and check % 2 == 0.
for n in range(2, 11, 2):
print(n)
โ
Task 4: Add Them All Up
Tricky
Use a loop to add up all the numbers from 1 to 10 and print the total (it's 55). Hint: start with total = 0 and add each number inside the loop.
total = 0
for n in range(1, 11):
total += n
print(total)
๐
Task 5: Free Play โ Rocket Launch
Creative
Make a countdown using a while loop! Start at 10, count down to 1, then blast off. Add your own emojis and messages.
๐๏ธ Extra Practice โ Drill Time!
Loops save you from writing the same line a hundred times. Grind through these and you will start seeing loops everywhere.
โ๏ธ
Task 6: Times Table
Easy
Print the 7 times table from 7 ร 1 to 7 ร 10, one line each, in the form 7 x 1 = 7. Use a for loop with range(1, 11).
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
for i in range(1, 11):
print(f"7 x {i} = {7 * i}")
๐งฎ
Task 7: Sum to 100
Easy
Add up every whole number from 1 to 100 with a loop and a total variable, then print the answer. (Legend says a schoolboy named Gauss did this in his head in seconds!)
5050
total = 0
for n in range(1, 101):
total += n
print(total)
โณ
Task 8: While Countdown
Easy
Use a while loop to count down from 5 to 1, one number per line, then print Blast off!. Do not forget to make the number smaller each time, or the loop will never stop!
5
4
3
2
1
Blast off!
n = 5
while n > 0:
print(n)
n -= 1
print("Blast off!")
๐ค
Task 9: Loop Over Letters
Easy
A for loop can walk through text one character at a time. Loop over "PYTHON" and print each letter on its own line.
P
Y
T
H
O
N
for letter in "PYTHON":
print(letter)
โญ๏ธ
Task 10: Skip and Stop
Medium
Loop over range(1, 11). Use continue to skip the number 5 entirely, and break to stop completely once you reach 8. Print the numbers that survive.
1
2
3
4
6
7
for n in range(1, 11):
if n == 5:
continue
if n == 8:
break
print(n)
๐
Task 11: Find the Biggest
Medium
Without using max(), find the largest number in scores = [12, 45, 7, 98, 33] with a loop and print it. Start with biggest = scores[0] and replace it whenever you meet something larger.
98
scores = [12, 45, 7, 98, 33]
biggest = scores[0]
for s in scores:
if s > biggest:
biggest = s
print(biggest)
โญ
Task 12: Growing Star Triangle
Medium
Print a triangle of stars 5 rows tall: one star on row 1, two on row 2, and so on. Hint: "*" * n repeats the star.
*
**
***
****
*****
for n in range(1, 6):
print("*" * n)
๐ฒ
Task 13: A Loop Inside a Loop
Tricky
Nested loops build grids. Print a 3 ร 3 grid of coordinate pairs like (1,1) โ three per line, separated by spaces. Hint: an inner loop with end=" ", then an empty print() to end the row.
for row in range(1, 4):
for col in range(1, 4):
print(f"({row},{col})", end=" ")
print()
๐ณ
Task 14: Boss Level: FizzBuzz
Boss
The most famous interview question ever! For numbers 1 to 20: print Fizz if the number divides by 3, Buzz if it divides by 5, FizzBuzz if it divides by both, otherwise the number itself. Check the both-case first!
1
2
Fizz
4
Buzz
โฆ
Buzz
for n in range(1, 21):
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)
๐งต
Task 15: Free Play โ Pattern Maker
Creative
Invent your own loop art: a diamond, a chessboard, a staircase, a spiral of emojis. Mix * repetition, nested loops and end="" until it looks great. ๐จ