The Analytical Partner
The Analytical Partner treats debugging like an investigation. Instead of asking the AI to fix the code immediately, the learner brings the broken attempt, the exact error or traceback, a short explanation of what they expected, and a focused question about where the failure may be coming from.
The example below simulates a student who already corrected one syntax problem but encountered a new runtime failure while debugging Topic #23.
Alice: $500
Traceback (most recent call last):
File "bonus_debug.py", line 17, in <module>
calculate_bonuses(employees_data)
File "bonus_debug.py", line 4, in calculate_bonuses
if rating >= 3:
TypeError: '>=' not supported between instances of 'str' and 'int'
def calculate_bonuses(employees):
for emp in employees:
rating = emp['rating']
if rating >= 3:
bonus = 500
elif rating >= 4:
bonus = 1000
else:
bonus = 0
print(f"{emp['name']}: ${bonus}")
employees_data = [
{'name': 'Alice', 'rating': 3},
{'name': 'Bob', 'rating': '5'},
{'name': 'Charlie', 'rating': 2}
]
calculate_bonuses(employees_data)
What happens next? When you paste this debugging request and your code immediately after the Mentor Prompt, you receive feedback like the example shown below.
Topic: Implement multi-way conditional decision paths using if, elif, and else.
Grab a fresh cup of coffee, because this is an excellent debugging scenario! You’ve run into not one, but two very classic bugs here: one that immediately crashes the program, and a “silent” logic bug hiding right behind it. You already noticed the logic smell in your question, which means your detective instincts are spot on. Let’s look at the logs.
First, let’s translate that traceback. Always read tracebacks from the bottom up.
The machine says: TypeError: '>=' not supported between instances of 'str' and 'int' on line 4.
In plain human English, Python is complaining: “Hey, you are asking me if a piece of text (a string) is mathematically greater than or equal to a number (an integer). I don’t know how to do math with words!”
Code Observations:
-
Observe the data structure being passed for Bob on line 13:
{'name': 'Bob', 'rating': '5'}. Notice how Alice’s3and Charlie’s2do not have quotes around them, but Bob’s5does. That makes it a string! -
Observe the logical structure on lines 4 to 7. You are checking
if rating >= 3first, and thenelif rating >= 4second. Remember thatif/elifchains act like a series of cascading doors. As soon as one door opens, the rest remain locked.
Socratic Question:
Let’s pretend Bob’s rating is successfully read as an integer 5. Python walks up to line 4 and asks: “Is 5 >= 3?”. The answer is True! So, Bob gets $500, and the program skips the elif entirely. Given this top-to-bottom flow, how can you rearrange your conditions so that a rating of 4 or 5 gets the $1000 they deserve without being “caught” by the $500 net first?
Next Step:
To fix the immediate crash, you either need to remove the quotes in your dictionary, or use the int() function on line 3 to sanitize the data before the comparison. Once you get past the crash, swap the order of your if and elif logic checks so the most exclusive/highest condition is evaluated first!
Phase 2: Full Solution
Here is the exact code with the minimal interventions needed to handle dirty data types and correctly route the conditional logic.
def calculate_bonuses(employees):
for emp in employees:
# FIX: Wrap the incoming data in int() to ensure we are always comparing numbers
rating = int(emp['rating'])
# FIX: Check the highest/most restrictive condition FIRST
if rating >= 4:
bonus = 1000
# FIX: This elif now safely catches the 3s, because the 4s and 5s were caught above
elif rating >= 3:
bonus = 500
else:
bonus = 0
print(f"{emp['name']}: ${bonus}")
employees_data = [
{'name': 'Alice', 'rating': 3},
{'name': 'Bob', 'rating': '5'},
{'name': 'Charlie', 'rating': 2}
]
calculate_bonuses(employees_data)
Fault Flow Analysis:
-
The Crash (
TypeError): The loop reached Bob. The dictionary held the string'5'. On line 4, Python evaluated'5' >= 3. Since Python is strongly typed, it refused to implicitly convert the string to a number and threw aTypeError, halting execution. -
The Logic Bug (Shadowing): Even if the program hadn’t crashed, Bob would have received the wrong bonus. Python evaluates
if/elifsequentially. Since5 >= 3is True, it would assignbonus = 500and exit the decision tree. Theelif rating >= 4was “shadowed” (unreachable for any number 4 or higher) because any number 4 or higher is also greater than 3.
Common Mistakes:
- Dirty Input Data: Data coming from APIs, CSVs, or user input is almost always in string format initially. Always sanitize/convert variables before throwing them into mathematical conditional checks.
-
Condition Shadowing: When checking ranges in a single direction (e.g., all
>=or all<=), placing the wider net first will trap everything. Rule of thumb: If checking>=, go from highest number to lowest. If checking<=, go from lowest number to highest.
Mini-Challenges:
-
The VIP Check: Add another condition so that if a rating is exactly
5, the employee gets a$2000bonus. Where must this condition sit in the current hierarchy to avoid being shadowed? -
Flip the Logic: Try rewriting the
if/elif/elseblock completely by checking<(less than) instead of>=. (Hint: You’ll start by checkingif rating < 3).
—
⚡ Coding5s Mentor System — Eliminating Epistemic Debt & Hunting Bugs
