Sharpen your Python debugging skills by identifying and correcting common mistakes in if‑elif‑else statements. Perfect for beginners practicing logic errors and syntax fixes.
Topic: if-elif-else
Fix the logical mistake in if-elif structure.
x = 7
if x > 5:
print("x is greater than 5")
elif x > 0:
print("x is positive")
Hint: 💡 `elif` is only checked if the `if` condition fails.
Issue: The second condition won't run because the first is already true.
x = 7
if x > 5:
print("x is greater than 5")
if x > 0:
print("x is positive")
Simplify and correct the final condition in if-elif chain.
score = 75
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
elif score < 70:
print("F")
Hint: 💡 Use `else` to catch all remaining cases.
Issue: The last condition is redundant.
score = 75
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
Identify and fix the syntax error caused by a missing colon.
if x > 5 # Missing colon here
print("X is greater than 5")
Hint: 💡 All `if` statements must end with a colon.
Issue: Syntax error due to missing colon.
if x > 5:
print("X is greater than 5")
Fix the indentation error in the following loop.
for i in range(0, 11):
if i % 2 == 0:
print(i)
Hint: 💡 `if` needs to be part of the `for` loop body.
Issue: Improper indentation of `if` statement.
for i in range(0, 11):
if i % 2 == 0:
print(i)
Add the missing colon in the if statement.
x = 10
if x > 5
print("x is greater than 5")
Hint: 💡 A colon is required after the condition.
Issue: Syntax error due to missing colon.
x = 10
if x > 5:
print("x is greater than 5")
Fix the incorrect indentation in the else block.
y = -3
if y > 0:
print("y is positive")
print("This is always printed")
else:
print("y is not positive")
Hint: 💡 Align all code blocks correctly under their headers.
Issue: `else` block is not indented.
y = -3
if y > 0:
print("y is positive")
print("This is always printed")
else:
print("y is not positive")
Fix the loop condition that causes the while loop to never execute.
count = 10
while count > 10:
print("This will never print")
count -= 1
Hint: 💡 Change the condition so the loop runs at least once.
Issue: Initial condition is false, loop never runs.
count = 10
while count >= 0:
print("Count:", count)
count -= 1
What will happen when you use `break` incorrectly with a `for-else` loop?
for i in range(5):
print(i)
else:
break
Hint: 💡 `break` cannot be used outside of a loop body.
Issue: `break` is used in an invalid position.
# Cannot use `break` directly in an `else` clause of a loop
for i in range(5):
print(i)
# Removed invalid `break`
Fix the infinite loop caused by a missing update in a while loop.
n = 10
while n > 0:
print(n)
Hint: 💡 The loop condition never changes.
Issue: Missing update to `n` causes an infinite loop.
n = 10
while n > 0:
print(n)
n -= 1
Fix the misuse of `continue` in a while loop that leads to an infinite loop.
i = 0
while i < 5:
if i == 2:
continue
print(i)
i += 1
Hint: 💡 Ensure the counter increments even when `continue` is used.
Issue: `i` is never incremented when `i == 2`, causing infinite loop.
i = 0
while i < 5:
if i == 2:
i += 1
continue
print(i)
i += 1
Fix the incorrect use of step in `range`.
for i in range(1, 10, -1):
print(i)
Hint: 💡 A negative step requires the start value to be greater than the stop value.
Issue: Loop never runs due to range direction mismatch.
for i in range(10, 0, -1):
print(i)
Fix the incorrect usage of the `else` block in the loop.
for i in range(5):
if i == 2:
break
else:
print(i)
Hint: 💡 The `else` here is tied to `if`, not the `for` loop.
Issue: The `else` block is misleading inside the loop.
for i in range(5):
if i == 2:
break
print(i)
else:
print("Loop completed without a break")
Handle the correct exception for division by zero.
try:
print(5 / 0)
except TypeError:
print("A TypeError occurred")
Hint: 💡 Understand what kind of error is raised by dividing by zero.
Issue: Catching the wrong exception type.
try:
print(5 / 0)
except ZeroDivisionError:
print("You can't divide by zero!")
Remove the unreachable print statement after a `continue`.
for i in range(5):
if i == 3:
continue
print("This will never be printed")
print(i)
Hint: 💡 Code after `continue` is never executed.
Issue: Unreachable code after `continue`.
for i in range(5):
if i == 3:
continue
print(i)
Refactor the logic to better reflect specific numeric ranges.
z = 20
if z > 30:
print("z is greater than 30")
elif z > 10:
print("z is greater than 10")
else:
print("z is less than 10")
Hint: 💡 Add more precise conditions if needed.
Issue: Ranges are too broad and overlap logically.
z = 20
if z > 30:
print("z is greater than 30")
elif z > 20:
print("z is greater than 20 but less than 30")
elif z > 10:
print("z is greater than 10")
else:
print("z is less than or equal to 10")
Understand how `pass` works and fix the unintended print behavior.
for i in range(5):
if i == 2:
pass
print("This should not print")
print(i)
Hint: 💡 `pass` does nothing; code after it still runs.
Issue: Misconception about how `pass` affects control flow.
for i in range(5):
if i == 2:
pass
print(i)
Rearrange exception handling to use `finally` correctly.
try:
result = 10 / 0
finally:
print("This will run no matter what.")
except ZeroDivisionError:
print("You can't divide by zero!")
Hint: 💡 `finally` must follow the `except` block.
Issue: Misplaced `finally` block causes syntax error.
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
finally:
print("This will run no matter what.")