0
0
Intro to Computingfundamentals~20 mins

Loops (repeating actions) in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
trace
intermediate
2:00remaining
Trace the output of a simple for loop

What will be printed by the following code?

Intro to Computing
for i in range(3):
    print(i * 2)
A0 2 4
B0 1 2
C2 4 6
D1 3 5
Attempts:
2 left
💡 Hint

Remember, range(3) produces 0, 1, 2.

🧠 Conceptual
intermediate
2:00remaining
Understanding while loop behavior

What will happen when this code runs?

Intro to Computing
count = 3
while count > 0:
    print(count)
    count -= 1
APrints 3 2 1 0 then stops
BPrints 3 2 1 then stops
CPrints 1 2 3 then stops
DRuns forever printing 3
Attempts:
2 left
💡 Hint

Check the condition count > 0 and how count changes.

Comparison
advanced
2:00remaining
Compare outputs of two loops

Which option correctly describes the difference between these two loops?

Loop 1:
for i in range(1, 5):
    print(i)

Loop 2:
i = 1
while i < 5:
    print(i)
    i += 1
ABoth print 1 2 3 4 but Loop 1 uses for, Loop 2 uses while
BLoop 1 prints 1 2 3 4, Loop 2 prints 1 2 3 4 5
CLoop 1 prints 0 1 2 3, Loop 2 prints 1 2 3 4
DLoop 1 runs forever, Loop 2 stops after printing 4
Attempts:
2 left
💡 Hint

Check the range and condition carefully.

identification
advanced
2:00remaining
Identify the error in loop code

What error will this code cause?

i = 0
while i < 3:
print(i)
i += 1
ANo error, prints 0 1 2
BTypeError because i is not a number
CSyntaxError because while is missing a colon
DIndentationError because print and increment are not indented
Attempts:
2 left
💡 Hint

Check the indentation of lines inside the loop.

🚀 Application
expert
3:00remaining
Determine final value after nested loops

What is the final value of total after running this code?

total = 0
for x in range(3):
    for y in range(2):
        total += x + y
A18
B12
C9
D15
Attempts:
2 left
💡 Hint

Calculate sums for each pair (x, y) and add carefully.