Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Codémon Power Multiplication Table
Create a power multiplication table for levels 1 to 3.
Hint : Use nested loops with range to create multiplication patterns. The outer loop controls the first number, the inner loop controls the second.
Exercise 2 — Training Session
What will be printed by this code?
session = 1
while session <= 5:
if session == 3:
session += 1
continue
print(session)
session += 11 2 4 51 2 3 4 51 2 51 2 4Hint : The continue statement skips the code to go to the next iteration. The break statement breaks out of the loop.
Exercise 3 — Magic Item Filter
Filter and print only the magic items from your inventory.
Hint : Remember the word used to check if a substring is part of a string.
Exercise 4 — Power Doubling
What's the correct way to write a loop that doubles power until it exceeds 100?
Hint : Only one solution is clean and correct here. Look thoroughly at the conditions.
Exercise 5 — Codémon Battle Lives
Create a battle loop where your Codémon lose a life every turn.
Hint : Fairly simple, while you're Pokémon is still alive, we keep training and losing lives.
Exercise 6 — String Pattern
What pattern will this nested loop print?
for stage in range(3):
for star in range(stage + 1):
print("★", end="")
print(" ", end="")★ ★★ ★★★★★★ ★★ ★★ ★ ★★★★Hint : Try counting both for loops in your head, you'll understand the pattern.
Exercise 7 — Even Power Levels
Create a loop that only prints even power levels for your Codémon until reaching level 10.
Hint : Break is used to exit the loop, while continue is used to skip the current iteration.
Exercise 8 — Level Sequence
Which range() will generate the sequence: 10, 7, 4, 1 for power-down levels?
range(10, 0, -3)range(10, 1, -3)range(10, 1, 3)range(10, -3, -1)Hint : Remember range includes the start value and excludes the end value. And negative steps count down.
Exercise 9 — Inventory Positions
Print items from your inventory with their position numbers.
Hint : Use enumerate to get both the index and the item value in one loop.
Exercise 10 — Nested Loop Break Behavior
What happens when break is used in a nested loop?
for day in range(3):
for session in range(3):
if session == 1:
break
print(f"{day}{session}", end=" ")00 10 2000 01 10 11 20 2100 01 0200Hint : Break only exits the inner loop, so the outer loop continues to the next iteration.
Exercise 11 — Codémon Level Up
Create a power leveling loop for your Codémon that stops at level 10.
Hint : Tips : x += 1 is equivalent to x = x + 1. Use it well!
Exercise 12 — Training Schedule
What will this training schedule loop print?
for hour in range(2, 10, 3):
print(hour, end=" ")2 5 82 3 4 5 6 7 8 93 6 92 4 6 8Hint : range() have 3 parameters: start, stop, and step.
Exercise 13 — Finding Magic Items
Create a nested loop that breaks when finding a magic item in treasure chests.
Hint : You need to look for one magic item per chest!
Exercise 14 — Healing Logic
What's wrong with this healing loop?
max_health = 100
health = 50
while health < 100:
health += 30
print(health)Hint : Try running the code in your head and compute the values.
Exercise 15 — Inventory Slots
Print your inventory items with their slot number (starting from 1).
Hint : Enumerate gives you both the index and the item.
Exercise 16 — Codémon Loop Operators
Match each operator with its meaning in Codémon battle loops.
andor<=!=Exercise 17 — Codémon Range Sequences
Match each range expression with the sequence it generates for Codémon power levels.
range(10, 0, -2)range(1, 8, 3)range(5, 1, -1)range(2, 9, 2)10, 8, 6, 4, 21, 4, 75, 4, 3, 22, 4, 6, 8Exercise 18 — Training
What will be printed by this training loop?
for day in range(2):
for hour in range(3):
if day == hour:
continue
print(f"{day}{hour}", end=" ")01 02 10 1200 01 02 10 11 1201 02 1000 11Hint : Try running the code in your head and you will understand it instantly.
Exercise 19 — Health Regeneration
Create a regeneration loop that caps health at maximum.
Hint : Capping health after regeneration ensures it does not exceed maximum health. Use an assignment to limit it.
Exercise 20 — Codémon Loop Control
Match each Codémon training scenario with the correct loop control statement.
continuebreakwhile energy > 0for i in range(5)Exercise 21 — Codémon Loop Outputs
Match each simple Codémon loop with its output.
range(3)range(1, 4)range(5, 1, -1)range(0, 6, 2)0 1 21 2 35 4 3 20 2 4Exercise 22 — Nested Loop Break Effect
What happens with nested exploration loops when you use break in an inner loop?
for area in range(3):
print(f"Area {area}")
for room in range(4):
if room == 2:
break
print(f"Room {room}")Hint : Break and Continue affects only the loop it is in, not the outer loops.
Exercise 23 — Codémon Loop Keywords
Match each Codémon loop keyword with its action.
breakcontinueforwhileExercise 24 — Codémon Battle Loop Logic
What will this Codémon battle loop print?
health = 100
while True:
damage = 30
if health <= 30:
break
health -= damage
if health <= 50:
continue
print(f"Health: {health}")Health: 70Health: 70 Health: 40Health: 70 Health: 40 Health: 10Health: 70 Health: 0Hint : Try running the code in your head.
Exercise 25 — Codémon Combo Damage
Create a Codémon combo damage loop where each hit does square damage (hit 1 = 1 damage, hit 2 = 4 damage, hit 3 = 9 damage).
Hint : ** means power in Python, but here, you might not need it..