0
0
Pythonprogramming~20 mins

While–else behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While–else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of while–else with break
What is the output of this Python code?
Python
i = 0
while i < 3:
    if i == 1:
        break
    print(i)
    i += 1
else:
    print('Done')
A0\n1
B0\nDone
C0\n1\nDone
D0
Attempts:
2 left
💡 Hint
Remember that the else block runs only if the loop completes without break.
Predict Output
intermediate
2:00remaining
While–else without break
What will this code print?
Python
count = 0
while count < 2:
    print(count)
    count += 1
else:
    print('Finished')
A0\n1\nFinished
B0\n1
CFinished
D0\nFinished
Attempts:
2 left
💡 Hint
The else block runs only if the loop ends normally without break.
Predict Output
advanced
2:00remaining
While–else with continue and break
What is the output of this code?
Python
n = 0
while n < 4:
    n += 1
    if n == 2:
        continue
    if n == 3:
        break
    print(n)
else:
    print('Loop ended')
A1\n4\nLoop ended
B1
C1\nLoop ended
D2
Attempts:
2 left
💡 Hint
Check how continue skips printing and break stops the loop.
Predict Output
advanced
2:00remaining
While–else with nested loops and break
What does this code print?
Python
x = 0
while x < 3:
    y = 0
    while y < 3:
        if x == y:
            break
        print(f'{x},{y}')
        y += 1
    else:
        print('Inner done')
    x += 1
else:
    print('Outer done')
AOuter done
B0,0\n1,0\n1,1\n2,0\n2,1\n2,2\nInner done\nOuter done
C1,0\n2,0\n2,1\nOuter done
D0,0\n1,0\n2,0\n2,1\nInner done\nOuter done
Attempts:
2 left
💡 Hint
Remember break exits the inner loop and else runs only if no break.
🧠 Conceptual
expert
2:00remaining
Understanding while–else behavior
Which statement about the while–else construct in Python is correct?
AThe else block runs only if the while loop completes without encountering a break statement.
BThe else block runs only if the while loop contains a continue statement.
CThe else block runs every time after the while loop, regardless of break.
DThe else block runs only if the while loop is skipped entirely.
Attempts:
2 left
💡 Hint
Think about when else runs in loops with break.