The code runs perfectly. It does not crash. It produces an output. **But the output is wrong.** These are the hardest to debug because Python doesn't throw an error message telling you what went wrong.
**Why it happens:** There is a flaw in your thinking or algorithm. You might have used the wrong formula, indented a line incorrectly so it runs inside a loop instead of outside, or confused variable names.
**How to handle it:**
- **Print Debugging:** Print variable values at different steps to see where they deviate from expectation.
- **Unit Tests:** Write separate small programs that test your functions with known inputs and expected outputs.
- **Walkthroughs:** "Rubber duck debugging"—explain line-by-line what your code _should_ be doing to an inanimate object.
```python
# --- BAD CODE (Logic Error) ---
# Goal: Calculate average of 10 and 20
# Error: Order of operations. Division happens before addition.
# Result: 10 + 10 = 20 (Wrong!)
average = 10 + 20 / 2
# --- GOOD CODE ---
# Result: 30 / 2 = 15 (Correct)
average = (10 + 20) / 2
```