Practice debugging Python code with common loop control mistakes. Identify and fix errors using break, continue in realistic scenarios.
Topic: loop-control-statements
Fix the loop that should exit when the user enters 'quit'.
while True:
user_input = input("Enter something (or 'quit' to exit): ")
if user_input = 'quit':
break
Hint: 💡 Check the comparison operator in the if statement.
Issue: Using assignment operator (=) instead of equality operator (==).
while True:
user_input = input("Enter something (or 'quit' to exit): ")
if user_input == 'quit':
break
Fix the logic error in this loop that skips elements.
numbers = [10, 20, 30, 40, 50]
for i in range(len(numbers)):
if i % 2 == 0:
continue
print(numbers[i])
Hint: 💡 The code is printing elements at odd indices, not even ones.
Issue: Condition is reversed - prints odd-indexed elements when it should print even-indexed.
numbers = [10, 20, 30, 40, 50]
for i in range(len(numbers)):
if i % 2 != 0:
continue
print(numbers[i])
Tutorials, Roadmaps, Bootcamps & Visualization Projects