Stage 2 – Debug

Coding5s / Pillar 1 / Stage 02

Debug

Stage 2 develops corrective competence. Instead of receiving another clean example, the learner works with intentionally flawed code, observes the evidence produced by the failure, identifies its cause, and repairs it while understanding why the original implementation was wrong.

INSPECT RUN DIAGNOSE EXPLAIN REPAIR
Reference output: Below is a complete example of a Stage 2 lesson generated through the Coding5s prompt architecture. The specific bugs, diagnostic guidance, and exercise structure vary according to the topic, technology, learner level, and configured technical rules.

🔹 Topic #23 | Stage 2 🐛 Debug | Python Core & Scripting | 📊 Level: Intermediate | 🌐 Coding5s.com

Concept: Conditional Branching (if, elif, else)

No synthetic data file was provided. Generating dummy data.

📥 From: Dave “CodeBreaker” Miller
📤 To: Python Core & Scripting Developer

Subject: I can’t even… the if-statements are cursed

Hey, I’ve been staring at this code since 3 AM and nothing works right. The logic branches are completely ignoring me and some of these loops just explode. I’m supposed to present this in 20 minutes. Fix it for me and I owe you a massive coffee and maybe my soul. Just make it output what the boss wants, please!

🚨 Your objective in this lesson: Copy the code blocks from each Challenge exactly as they are into your IDE. Then fix all errors until you get the exact Expected Output when running the program.

Challenge 1️⃣: Employee Bonus Calculator (Score 3)

🎯 Objective: Calculate bonus based on performance rating using a loop over a list of dictionaries.

💻 Expected Output:

Plaintext
Alice: $500
Bob: $1000
Charlie: $0

Broken Code:

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

Challenge 2️⃣: User Access Role Assigner (Score 4)

🎯 Objective: Assign system roles based on user department and years of service without overwriting higher privileges.

💻 Expected Output:

Plaintext
John -> SuperAdmin
Sara -> HR_Manager
Mike -> Guest

Broken Code:

Python
def assign_roles(users_list):
    for user in users_list:
        dept = user['deprtment']
        years = user['years']
        
        if dept == "IT" and years >= 5:
            role = "SuperAdmin"
        elif dept = "HR" and years >= 3:
            role = "HR_Manager"
        if dept == "IT" and years < 5:
            role = "Tech"
        else:
            role = "Guest"
            
        print(f"{user['name']} -> {role}")

users = [
    {'name': 'John', 'department': 'IT', 'years': 6},
    {'name': 'Sara', 'department': 'HR', 'years': 4},
    {'name': 'Mike', 'department': 'Sales', 'years': 2}
]
assign_roles(users)

Challenge 3️⃣: Temperature Status Monitor (Score 4)

🎯 Objective: Classify system temperatures correctly as Critical, Warning, or Normal using exclusive thresholds.

💻 Expected Output:

Plaintext
Sensor A: Normal
Sensor B: Warning
Sensor C: Critical

Broken Code:

Python
def check_temperatures(sensor_data):
    for sensor in sensor_data:
        temp = sensor['temp']
        
        if temp > 100:
            status = "Critical"
        else if temp >= 80 or temp <= 100:
            status = "Warning"
        else:
            status = "Normal"
            
        print(sensor['name'] + ": " + status)

sensors = [
    {'name': 'Sensor A', 'temp': 75},
    {'name': 'Sensor B', 'temp': '90'},
    {'name': 'Sensor C', 'temp': 105}
]
check_temperatures(sensors)

Challenge 4️⃣: Subscription Discount Router (Score 5)

🎯 Objective: Apply discount percentages exclusively based on subscription tier for active users.

💻 Expected Output:

Plaintext
User1 gets a 20% discount.
User2 gets a 0% discount.
User3 gets a 10% discount.

Broken Code:

Python
def calculate_discounts(users_info):
    for u in users_info:
        tier = u['tier']
        
        if u['active'] == False:
            discount = 0
        elif tier == "premium":
            discount = 20
        elif tier == "Basic":
        discount = 10
        
        print(f"{u['name']} gets a {discount}% discount.")

customers = [
    {'name': 'User1', 'tier': 'Premium', 'active': True},
    {'name': 'User2', 'tier': 'Free', 'active': True},
    {'name': 'User3', 'tier': 'Basic', 'active': True}
]
calculate_discounts(customers)

⚡ Coding5s System – Learn Programming by Writing Code – Coding5S.com

Scroll to Top