Mentor Stage 2

Coding5s / Pillar 2 / Stage 02 Mentor

The Analytical Partner

Debug Mentor

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.

RUN READ TRACEBACK EXPLAIN ASK FIND ROOT CAUSE
HOW TO USE THIS MENTOR
01 Generate and copy the Stage 2 Mentor Prompt from the Coding5s Creator Kit.
02 Run your broken code and keep the exact error message or traceback produced by the program.
03 Paste the Mentor Prompt into your AI chat, followed by your code, the traceback, and a brief explanation of what you were trying to achieve.
04 Ask one specific debugging question. The goal is to investigate the root cause before asking for the corrected code.
EXAMPLE / STUDENT DEBUG REQUEST

The example below simulates a student who already corrected one syntax problem but encountered a new runtime failure while debugging Topic #23.

Student: I’m seriously stuck on this one. I already fixed the missing colon because Python wouldn’t even start, and now Alice prints correctly but the program crashes as soon as it reaches Bob. I’ve been staring at these conditions and I can’t see why the same comparison works for one employee and fails for the next.

Question: The traceback ends with a TypeError. What should I inspect first to find the root cause, and is the order of my if and elif checks also causing a separate logic problem?

Python / Traceback
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'
Python / Student Attempt
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)
Reference interaction: The student provides the broken code, the actual traceback, what happened before the crash, and their current suspicion. This gives the mentor enough evidence to translate the machine error, trace the data flow, and guide the learner toward the root cause instead of immediately replacing their work with a solution.

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.

🕵️‍♂️ Elite Mentor Stage 2 | Topic #23 | Debug | Python Core & Scripting | 📊 Level: Intermediate

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’s 3 and Charlie’s 2 do not have quotes around them, but Bob’s 5 does. That makes it a string!
  • Observe the logical structure on lines 4 to 7. You are checking if rating >= 3 first, and then elif rating >= 4 second. Remember that if/elif chains 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!

🛑 THE EXPLANATION GATE 🛑 Before scrolling down to see the Master Code, articulate the ROOT CAUSE of the crash. Copy-pasting a fix without understanding the bug builds Epistemic Debt.

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.

Python
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:

  1. 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 a TypeError, halting execution.
  2. The Logic Bug (Shadowing): Even if the program hadn’t crashed, Bob would have received the wrong bonus. Python evaluates if/elif sequentially. Since 5 >= 3 is True, it would assign bonus = 500 and exit the decision tree. The elif rating >= 4 was “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:

  1. The VIP Check: Add another condition so that if a rating is exactly 5, the employee gets a $2000 bonus. Where must this condition sit in the current hierarchy to avoid being shadowed?
  2. Flip the Logic: Try rewriting the if/elif/else block completely by checking < (less than) instead of >=. (Hint: You’ll start by checking if rating < 3).

Coding5s Mentor System — Eliminating Epistemic Debt & Hunting Bugs

Scroll to Top