🔄 Chapter 5: Repeating Actions (Loops)

Test your understanding of Python's automation powerhouse!

🎯 Multiple Choice Questions

1. What is the main purpose of loops in programming?

  • To make decisions in code
  • To repeat a set of instructions multiple times
  • To store data in variables
  • To create functions
Correct! Loops are designed to repeat instructions, automating tedious tasks and letting Python do the repetitive work for you.

2. Which loop is best when you have a collection of items to process?

  • while loop
  • for loop
  • if loop
  • break loop
Correct! A for loop is perfect when you have a collection (like a list) and want to do something with each item in that collection.

3. What does range(5) generate?

  • Numbers 1, 2, 3, 4, 5
  • Numbers 0, 1, 2, 3, 4
  • Numbers 1, 2, 3, 4
  • The number 5
Correct! range(5) generates numbers from 0 up to (but not including) 5, so: 0, 1, 2, 3, 4.

4. What is an infinite loop?

  • A loop that runs very fast
  • A loop that never stops running
  • A loop that runs backwards
  • A loop that prints infinite numbers
Correct! An infinite loop never stops running because the condition never becomes False - like a hamster wheel that never stops spinning!

5. What does the break statement do in a loop?

  • Pauses the loop for a few seconds
  • Exits the loop immediately
  • Skips to the next iteration
  • Restarts the loop from the beginning
Correct! The break statement exits the loop immediately, like an emergency exit door - once you use it, you're out!
🔍 Code Analysis

6. What will this code output?

for i in range(3): print(f"Count: {i}")
  • Count: 1
    Count: 2
    Count: 3
  • Count: 0
    Count: 1
    Count: 2
  • Count: 3
  • Nothing will print
Correct! range(3) generates 0, 1, 2, so the loop prints "Count: 0", "Count: 1", "Count: 2".

7. What will happen with this while loop?

count = 0 while count < 3: print("Hello!") count += 1
  • Prints "Hello!" once
  • Prints "Hello!" three times
  • Prints "Hello!" forever
  • Causes an error
Correct! The loop starts with count=0, runs while count<3, and increments count each time, so it prints "Hello!" exactly 3 times.

8. What does this code demonstrate?

numbers = [1, 2, 3, 4, 5] for num in numbers: if num == 3: continue print(num)
  • Prints all numbers
  • Prints 1, 2, 4, 5 (skips 3)
  • Prints only the number 3
  • Stops at number 3
Correct! The continue statement skips the rest of the current iteration when num equals 3, so it prints 1, 2, 4, 5.

9. What's wrong with this while loop?

temperature = 75 while temperature > 70: print("It's warm!")
⚠️ Warning: This is a dangerous loop pattern!
  • Nothing is wrong
  • It will create an infinite loop
  • The condition is backwards
  • It should use a for loop instead
Correct! This creates an infinite loop because temperature never changes inside the loop, so the condition (75 > 70) is always True!
✨ F-strings and Formatting

10. What does the 'f' in f-strings stand for?

  • Fast
  • Formatted
  • Function
  • Final
Correct! The 'f' stands for "formatted" - f-strings are formatted string literals that let you easily include variables in your text.

11. What does \n do in a string?

  • Creates a tab space
  • Creates a new line
  • Creates a backslash
  • Nothing special
Correct! \n is an escape sequence that creates a new line, like pressing Enter in your output.

12. What will this f-string output if name="Alex" and age=25?

print(f"Hello {name}, you are {age} years old!")
  • Hello {name}, you are {age} years old!
  • Hello Alex, you are 25 years old!
  • Hello name, you are age years old!
  • An error will occur
Correct! F-strings automatically replace {name} with "Alex" and {age} with "25", creating personalized output.
🪆 Nested Loops

13. What are nested loops?

  • Loops that run backwards
  • Loops inside other loops
  • Loops that never end
  • Loops that run very fast
Correct! Nested loops are loops inside other loops - like Russian nesting dolls (matryoshka), where one contains another.

14. How many times will "Hello" be printed?

for i in range(2): for j in range(3): print("Hello")
  • 2 times
  • 3 times
  • 5 times
  • 6 times
Correct! The outer loop runs 2 times, and for each of those times, the inner loop runs 3 times: 2 × 3 = 6 times total.
✏️ Fill in the Blanks

15. Complete this shopping cart total calculator:

prices = [10.99, 5.50, 8.25, 12.00] total = 0 price prices: total price print(f"Total: ${total}")

Fill in: The keywords needed to create a loop that adds up all prices.

Correct answers: "for", "in", "+=" - This creates a loop that goes through each price and adds it to the total.
✅ True or False

16. A for loop always runs a fixed number of times.

  • True
  • False
Correct! False! A for loop can use break to exit early, so it doesn't always run a fixed number of times.

17. The continue statement exits the entire loop.

  • True
  • False
Correct! False! The continue statement only skips the current iteration and moves to the next one. It's break that exits the entire loop.

18. range(1, 5) generates the numbers 1, 2, 3, 4, 5.

  • True
  • False
Correct! False! range(1, 5) generates 1, 2, 3, 4 - it stops before the ending number (5).

🎉 Quiz Complete!

Your Score: 0/18

Keep practicing!