Learn with Yasir

Share Your Feedback

Find and Fix Mistakes – Python if‑elif‑else Practice for Beginners

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 & Find Questions

🟢 Beginner Fix & Find

  1. 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.

    🔍 View Issue & Fixed Solution

    Issue: The second condition won't run because the first is already true.

    ✅ Fixed Solution

    x = 7
    
    if x > 5:
        print("x is greater than 5")
    if x > 0:
        print("x is positive")
    

  2. 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.

    🔍 View Issue & Fixed Solution

    Issue: The last condition is redundant.

    ✅ Fixed Solution

    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")
    

  3. 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.

    🔍 View Issue & Fixed Solution

    Issue: Syntax error due to missing colon.

    ✅ Fixed Solution

    if x > 5:
        print("X is greater than 5")
    

  4. 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.

    🔍 View Issue & Fixed Solution

    Issue: Improper indentation of `if` statement.

    ✅ Fixed Solution

    for i in range(0, 11):
        if i % 2 == 0:
            print(i)
    

  5. 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.

    🔍 View Issue & Fixed Solution

    Issue: Syntax error due to missing colon.

    ✅ Fixed Solution

    x = 10
    
    if x > 5:
        print("x is greater than 5")
    

  6. 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.

    🔍 View Issue & Fixed Solution

    Issue: `else` block is not indented.

    ✅ Fixed Solution

    y = -3
    
    if y > 0:
        print("y is positive")
        print("This is always printed")
    else:
        print("y is not positive")
    

  7. 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.

    🔍 View Issue & Fixed Solution

    Issue: Initial condition is false, loop never runs.

    ✅ Fixed Solution

    count = 10
    
    while count >= 0:
        print("Count:", count)
        count -= 1
    

  8. 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.

    🔍 View Issue & Fixed Solution

    Issue: `break` is used in an invalid position.

    ✅ Fixed Solution

    # Cannot use `break` directly in an `else` clause of a loop
    for i in range(5):
        print(i)
    # Removed invalid `break`
    

🟡 Intermediate Fix & Find

  1. 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.

    🔍 View Issue & Fixed Solution

    Issue: Missing update to `n` causes an infinite loop.

    ✅ Fixed Solution

    n = 10
    
    while n > 0:
        print(n)
        n -= 1
    

  2. 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.

    🔍 View Issue & Fixed Solution

    Issue: `i` is never incremented when `i == 2`, causing infinite loop.

    ✅ Fixed Solution

    i = 0
    
    while i < 5:
        if i == 2:
            i += 1
            continue
        print(i)
        i += 1
    

  3. 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.

    🔍 View Issue & Fixed Solution

    Issue: Loop never runs due to range direction mismatch.

    ✅ Fixed Solution

    for i in range(10, 0, -1):
        print(i)
    

  4. 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.

    🔍 View Issue & Fixed Solution

    Issue: The `else` block is misleading inside the loop.

    ✅ Fixed Solution

    for i in range(5):
        if i == 2:
            break
        print(i)
    else:
        print("Loop completed without a break")
    

  5. 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.

    🔍 View Issue & Fixed Solution

    Issue: Catching the wrong exception type.

    ✅ Fixed Solution

    try:
        print(5 / 0)
    except ZeroDivisionError:
        print("You can't divide by zero!")
    

  6. 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.

    🔍 View Issue & Fixed Solution

    Issue: Unreachable code after `continue`.

    ✅ Fixed Solution

    for i in range(5):
        if i == 3:
            continue
        print(i)
    

  7. 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.

    🔍 View Issue & Fixed Solution

    Issue: Ranges are too broad and overlap logically.

    ✅ Fixed Solution

    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")
    

  8. 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.

    🔍 View Issue & Fixed Solution

    Issue: Misconception about how `pass` affects control flow.

    ✅ Fixed Solution

    for i in range(5):
        if i == 2:
            pass
        print(i)
    

  9. 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.

    🔍 View Issue & Fixed Solution

    Issue: Misplaced `finally` block causes syntax error.

    ✅ Fixed Solution

    try:
        result = 10 / 0
    except ZeroDivisionError:
        print("You can't divide by zero!")
    finally:
        print("This will run no matter what.")
    

🔴 Advanced Fix & Find

📚 Related Resources