Challenge - 5 Problems
Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop
What is the output of this Python code that uses a for loop to print numbers?
Python
for i in range(3): print(i)
Attempts:
2 left
💡 Hint
Remember, range(3) starts at 0 and goes up to but not including 3.
✗ Incorrect
The range(3) generates numbers 0, 1, and 2. The loop prints each number on its own line.
🧠 Conceptual
intermediate1:30remaining
Why use loops instead of repeating code?
Why do programmers use loops instead of writing the same code multiple times?
Attempts:
2 left
💡 Hint
Think about what happens if you want to change repeated code.
✗ Incorrect
Loops let you write code once and run it many times, making programs shorter and easier to update.
❓ Predict Output
advanced2:30remaining
Output of nested loops
What is the output of this nested loop code?
Python
for i in range(2): for j in range(2): print(i, j)
Attempts:
2 left
💡 Hint
The outer loop runs twice, and for each outer loop, the inner loop runs twice.
✗ Incorrect
The outer loop variable i goes 0 then 1. For each i, j goes 0 then 1, printing pairs in order.
❓ Predict Output
advanced2:00remaining
Loop with break statement output
What will this code print?
Python
for i in range(5): if i == 3: break print(i)
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3.
✗ Incorrect
The loop prints 0,1,2 then stops before printing 3 because of break.
🧠 Conceptual
expert1:30remaining
Why loops are essential for repetitive tasks
Which statement best explains why loops are essential in programming?
Attempts:
2 left
💡 Hint
Think about what happens when you want to do the same thing many times.
✗ Incorrect
Loops let programmers repeat actions easily and efficiently, which is key for many tasks.