Debugging Conditional Logic

Code Examples

Example 1

Wrong Comparison Operator (Common Bug). Using = (assignment) instead of == (comparison) is a very common mistake. The = tries to assign a value, while == compares values. Python will give you a SyntaxError for this mistake, making it easier to spot.

āŒ Buggy Code:
password = "secret123"
if password = "secret123":  # Wrong! Using = instead of ==
    print("Access granted!")

āœ… Fixed Code:
password = "secret123"
if password == "secret123":  # Correct! Using == for comparison
    print("Access granted!")

Example 2

Indentation Error. Python requires proper indentation to know which code belongs inside the if statement. Without indentation, you'll get an IndentationError. Always indent code inside if blocks.

āŒ Buggy Code:
age = 20
if age >= 18:
print("You can vote!")  # Wrong! Not indented

āœ… Fixed Code:
age = 20
if age >= 18:
    print("You can vote!")  # Correct! Properly indented

Example 3

Logic Error - Wrong Order. The buggy version always prints "Grade: D" for any score >= 60, because Python stops at the first true condition. Always order conditions from most specific to least specific (highest to lowest for ranges).

āŒ Buggy Code:
score = 85
if score >= 60:
    print("Grade: D")
elif score >= 70:
    print("Grade: C")
elif score >= 80:
    print("Grade: B")
elif score >= 90:
    print("Grade: A")

āœ… Fixed Code:
score = 85
if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")

Example 4

Using Print for Debugging. Adding debug prints helps you see what values your variables actually contain and what your comparisons evaluate to. This reveals that "Alice" ≠ "alice" (case matters!) and helps identify the bug.

username = "Alice"
password = "wrong"

print(f"Username: '{username}'")  # Debug print
print(f"Password: '{password}'")  # Debug print
print(f"Username check: {username == 'alice'}")  # Debug print
print(f"Password check: {password == 'secret123'}")  # Debug print

if username == "alice" and password == "secret123":
    print("Login successful!")
else:
    print("Login failed!")

Example 5

Unreachable Code (Dead Code). In the buggy version, the third condition can never be reached because any temperature > 25 would already be caught by the first condition (> 20). Always check that all conditions are reachable.

āŒ Buggy Code:
temperature = 25
if temperature > 20:
    print("It's warm!")
elif temperature > 15:
    print("It's mild!")
elif temperature > 25:  # This will NEVER execute!
    print("It's hot!")

āœ… Fixed Code:
temperature = 25
if temperature > 25:
    print("It's hot!")
elif temperature > 20:
    print("It's warm!")
elif temperature > 15:
    print("It's mild!")

Example 6

Missing Edge Cases. The buggy version crashes if the user enters non-numeric input, and doesn't handle the string-to-number conversion. The fixed version handles invalid inputs and edge cases like negative ages.

āŒ Buggy Code:
age = input("Enter your age: ")
if age >= 18:
    print("Adult")
else:
    print("Minor")

āœ… Fixed Code:
age_input = input("Enter your age: ")
try:
    age = int(age_input)
    if age >= 18:
        print("Adult")
    elif age >= 0:
        print("Minor")
    else:
        print("Invalid age: negative number")
except ValueError:
    print("Invalid input: please enter a number")