Python · Loops
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.
____ level ____ range(1, 4):
____ multiplier ____ range(1, 4):
print(f"{level}, {multiplier}, {level * multiplier}")
Options: for in for in of
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 += 1
Options
• ```1 2 4 5```
• ```1 2 3 4 5```
• ```1 2 5```
• ```1 2 4```
Hint : 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.
inventory = ["crystal_sword", "magic_potion", "magic sword", "etc"]
____ item ____ inventory:
if "magic" ____ item:
print(item)
Options: for in in for if of while
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?
Options
• power = 2
while power <= 100:
power = power * 2
print(power)
• power = 2
for i in range(100):
power = power * 2
• power = 2
while True:
if power > 100:
power = power * 2
• power = 2
while power < 100:
power + power
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.
lives = 3
____ lives ____ 0:
print(f"Your Codémon has {lives} lives left")
lives = lives ____ 1
Options: while > - + for <=
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="")
Options
• ```★ ★★ ★★★```
• ```★★★ ★★ ★```
• ```★ ★ ★```
• ```★★★```
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.
level = 0
____ level < 10:
level += 1
if level % 2 ____ 0:
____
print(level)
Options: while != continue for == break
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?
Options
• ```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.
items = ["crystal_sword", "healing_potion", "magic_wand"]
____ i, item ____ enumerate(items):
print(f"Position {i+1}: {item}")
Options: for in while of , =
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=" ")
Options
• ```00 10 20```
• ```00 01 10 11 20 21```
• ```00 01 02```
• ```00```
Hint : 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.
level = 1
____ level ____ 10:
print(f"Level up! Now level {level}")
level ____ 1
Options: while <= += = -=
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=" ")
Options
• ```2 5 8```
• ```2 3 4 5 6 7 8 9```
• ```3 6 9```
• ```2 4 6 8```
Hint : 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.
chests = [["gold", "magic_ring"], ["potion", "sword"]]
found = False
____ i ____ range(len(chests)):
____ item ____ chests[i]:
if "magic" in item:
found = True
print(item)
____
Options: for in for in break continue while == !=
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)
Options
• Health can exceed max_health
• The loop will never end
• Health will never reach 100 or more
• The increment is too small
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).
inventory = ["crystal_sword", "healing_potion", "power_gem"]
____ slot, item ____ ____(inventory):
print(f"Slot {slot+1}: {item}")
Options: for in enumerate range , of
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.
Items to match
• and
• or
• <=
• !=
Definitions
• Both conditions must be true
• At least one condition must be true
• Less than or equal to
• Not equal to
Exercise 17 — Codémon Range Sequences
Match each range expression with the sequence it generates for Codémon power levels.
Items to match
• range(10, 0, -2)
• range(1, 8, 3)
• range(5, 1, -1)
• range(2, 9, 2)
Definitions
• 10, 8, 6, 4, 2
• 1, 4, 7
• 5, 4, 3, 2
• 2, 4, 6, 8
Exercise 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=" ")
Options
• ```01 02 10 12```
• ```00 01 02 10 11 12```
• ```01 02 10```
• ```00 11```
Hint : 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.
health = 50
max_health = 100
____ health < max_health:
health += 20
print(health)
if health ____ max_health:
health ____ max_health
print("health capped", health)
Options: while > = == for in
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.
Items to match
• Skip weak opponents, continue training
• Found legendary Codémon, stop searching
• Keep training until energy depleted
• Train for exactly 5 sessions
Definitions
• continue
• break
• while energy > 0
• for i in range(5)
Exercise 21 — Codémon Loop Outputs
Match each simple Codémon loop with its output.
Items to match
• range(3)
• range(1, 4)
• range(5, 1, -1)
• range(0, 6, 2)
Definitions
• 0 1 2
• 1 2 3
• 5 4 3 2
• 0 2 4
Exercise 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}")
Options
• It only breaks the inner loop, outer loop continues
• It breaks both loops
• It only breaks the outer loop
• It causes an error
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.
Items to match
• break
• continue
• for
• while
Definitions
• Exit the loop immediately
• Skip to next iteration
• Loop through a sequence
• Loop while condition is true
Exercise 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}")
Options
• ```Health: 70```
• ```Health: 70 Health: 40```
• ```Health: 70 Health: 40 Health: 10```
• ```Health: 70 Health: 0```
Hint : 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).
total_damage = 0
____ hit ____ range(1, 4):
damage = hit ____ hit
total_damage += damage
print(f"Hit {hit}: {damage} damage")
Options: for in * ** + /
Hint : ```**``` means power in Python, but here, you might not need it..
Back to the course