Introduction
Not all Python bugs crash your program — some quietly sabotage your logic without raising any errors. These are logic mistakes: code that runs but doesn’t do what you expected. They can be frustrating because Python doesn’t warn you, and your program continues… just incorrectly.
The good news? With careful practice, you can learn to recognize and fix these issues before they cause deeper problems.
What Causes This Error?
1. Using =
Instead of ==
It’s easy to confuse assignment (=
) with comparison (==
). This mistake won’t throw an error, but will change your variable instead of checking it.
if x = 5: # SyntaxError
In contrast, if used inside a different context like:
x = 5
if x: # Always True unless x is 0, None, etc.
print("x is True")
This may behave unexpectedly depending on your assumptions.
2. Misunderstanding Boolean Expressions
Consider:
if x == 1 or 2:
print("True!")
You might think this checks if x
is 1 or 2, but it doesn’t. It always returns True
because 2
is a truthy value.
3. Off-by-One Errors
Looping over a range but excluding or including one too many items is common:
for i in range(10):
print(i) # Prints 0 to 9, not 10
If you meant to include 10, this code silently misses it.
4. Overwriting Built-in Names
Accidentally using Python’s built-in names like list
, str
, or sum
for your own variables:
list = [1, 2, 3] # You’ve just overwritten the built-in list()
Later when you try to use list()
elsewhere, it won’t work as expected.
How to Fix It
Fix 1: Double-check logical expressions
Rewrite compound conditions clearly:
# Bad
if x == 1 or 2:
# Good
if x == 1 or x == 2:
This ensures both comparisons are explicitly checked.
Fix 2: Use Clear and Descriptive Variable Names
Avoid overwriting built-ins:
# Bad
sum = 5 + 10
# Good
total = 5 + 10
Using descriptive names like total
, data_list
, or user_input
avoids shadowing core Python functions.
Fix 3: Test Edge Cases
Always run a few tests outside your loop range:
# Expected to go up to 10?
for i in range(11): # Use 11 to include 10
print(i)
Testing around your loop boundaries helps catch logic oversights.
Real Example
Buggy Version
def is_valid_age(age):
if age >= 18 or 21:
return True
return False
print(is_valid_age(17)) # Returns True (unexpected!)
Fixed Version
def is_valid_age(age):
if age >= 18 or age == 21:
return True
return False
print(is_valid_age(17)) # Returns False (as expected)
The original code returned True
regardless of age
, due to the misuse of or
.
Tips to Prevent This Error
1. Write Unit Tests
Small test functions help verify your logic behaves as intended.
2. Print Intermediate Results
Temporarily use print()
or a debugger like pdb
to track variable values and decisions.
3. Read Your Code Out Loud
Sometimes, hearing the logic reveals mistakes you don’t spot visually.
Conclusion
Logic bugs can be sneaky, but you can outsmart them with awareness and practice. Keep testing, breaking, and learning from your code.
Bookmark this post so you can revisit these common pitfalls whenever something just doesn’t feel right — even if Python says it’s fine.