Task 2 · Debugging
up to +100 XP
Stop 2.1 · Task 2 · 5 min

Debugging

Identify and fix defects in code

Learning Objectives:
  • Understand common types of errors in Python code
  • Learn systematic debugging techniques
  • Practice identifying syntax, logic, and runtime errors
  • Apply fixes to non-functioning code

Understanding the Task

In Task 2 of your exam, you will be given non-functioning code that should meet specific requirements. Your job is to:

Types of Errors

1. Syntax Errors

These prevent the code from running at all. Python will show an error message immediately.

2. Logic Errors

The code runs but produces incorrect results.

3. Runtime Errors

The code runs but crashes during execution.

Example: T-Learn Automotives Commission Calculator

Below is typical code you might encounter in the exam. It has several errors that need fixing.

Requirements

Non-Working Code (with errors)

from datetime import date

sales_list = ()

def get_id():
    flag = True
    while flag:
        employee_ID = input("Please enter the employee ID number: ")
        if len(employee_ID) == 8:
            print ("ID must be 8 digits long. Please try again.")
        else:
            print("ID number accepted")
            return employee_ID
          

def get_car_sales ():
    flag = True
    while flag: 
        num_cars = input("Please enter the number of cars sold: ")
        if num_cars.isdigit() == True:
            num_cars = int(num_cars)
            flag = False
        else:
            print("Input can only be a number. Please try again.")

    for i in range(num_cars):
        car_value = int(input("Please enter the value of car number {}. £".format(i + 1)))
        sales_list.append(car_value)
    
def calc_total():
    total = 0
    for i in sales_list:
        total = i
    
    return total

def calc_bonus(total):
    
    if len(sales_list) > 6:
        bonus = 500
    else:
        bonus = 0    
     
    if total >= 135000:
        bonus = bonus + (total * 0.4)
    else:
        bonus = bonus
    
    return bonus

print("Welcome to T-Learn Automotives commission calculation tool. ")
name = input("Please enter employee name: ")
id = get_id()
get_car_sales()
total_value = calc_totol()
commission = total_value * 0.05
bonus = calc_bonus(total_value)
today = date.today()

print("##############################")
print("########## Summary ##########")
print('')
print("Name: {}".format(name))
print("ID number:")
print("Total value of sales: £{}".format(total_value))
print("Commission earned: £{}".format(commission))
print("Bonus earned: £{}".format(bonus))
print("Date: {}".format(today))



with open('sales_summaries.txt', 'a') as saes: 
    sales.write("Name: {}\n".format(name))
    sales.write("ID number: {}\n".format(id))
    sales.write("Total value of sales: £{}\n".format(total_value))
    sales.write("Commission earned: £{}\n".format(commission))
    sales.write("Bonus earned: £{}\n".format(bonus))
    sales.write("Date: {}\n".format(today))
    sales.write("##################\n")

Systematic Debugging Process

Step 1: Read the Requirements

Carefully read what the code should do. Understand all the business rules and validation requirements.

Step 2: Run the Code

Try to run the code and note any immediate errors. Python will tell you the line number and type of error.

Step 3: Fix Syntax Errors First

Start with errors that prevent the code from running. These are usually easiest to spot.

Step 4: Test with Sample Data

Once the code runs, test it with known inputs and check if outputs are correct.

Step 5: Check Logic Against Requirements

Compare each part of the code to the requirements. Does it do what it's supposed to?

Step 6: Test Edge Cases

Test boundary conditions (e.g., exactly 6 cars, exactly £135,000).

Common Errors in the Example Code

Error 1: Wrong Data Structure

sales_list = () creates a tuple, not a list. Tuples are immutable and don't have an append method.

Fix: sales_list = []

Error 2: Incorrect Validation Logic

The condition if len(employee_ID) == 8: prints an error when the ID IS 8 digits. This is backwards.

Fix: if len(employee_ID) != 8:

Error 3: Wrong Calculation

In calc_total(), total = i replaces the total instead of adding to it.

Fix: total += i or total = total + i

Error 4: Wrong Condition

if len(sales_list) > 6: should be >= 6 because bonus is for "six or more" cars.

Fix: if len(sales_list) >= 6:

Error 5: Wrong Percentage

bonus = bonus + (total * 0.4) calculates 40% instead of 4%.

Fix: bonus = bonus + (total * 0.04)

Error 6: Typo in Function Name

total_value = calc_totol() has a typo - should be calc_total().

Fix: total_value = calc_total()

Error 7: Missing Variable in Print

print("ID number:") doesn't print the actual ID.

Fix: print("ID number: {}".format(id))

Error 8: Wrong Variable Name

with open('sales_summaries.txt', 'a') as saes: but then uses sales.write().

Fix: Change saes to sales or change all sales.write() to saes.write()

Documentation Tips

When documenting your debugging process:

Best Practices

Quick check · 15 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 15

What is the correct fix for sales_list = ()?

Question 2

Question 2 of 15

How do you fix the reversed ID validation logic?

Question 3

Question 3 of 15

What is the fix for total = i in the calc_total() function?

Question 4

Question 4 of 15

How do you fix the "six or more" cars bonus condition?

Question 5

Question 5 of 15

What is the correct percentage for the additional bonus calculation?

Question 6

Question 6 of 15

What is the typo in the function call?

Question 7

Question 7 of 15

How do you fix the missing ID number in the print statement?

Question 8

Question 8 of 15

What is the variable name mismatch in the file output?

Question 9

Question 9 of 15

Which type of errors should you fix FIRST when debugging?

Question 10

Question 10 of 15

What should you do after fixing each error?

Question 11

Question 11 of 15 · Coding

Debug it: bonus(cars_sold) should give a £500 bonus for selling 6 or more cars, otherwise 0. It has a syntax error, so it won’t run. Fix it.

Open the IDE
Hint

Look at the end of the if line. Every if, for, while and def line ends with a colon.

Question 12

Question 12 of 15 · Coding

Debug it: commission(value) should return 5% commission on a sale. It runs, but the answers are far too big (a logic error). Fix it.

Open the IDE
Hint

5% as a decimal is 0.05.

Question 13

Question 13 of 15 · Coding

Debug it: a salesperson gets a bonus when their total sales are £135,000 or more. gets_bonus(total) gets the boundary wrong. Fix it.

Open the IDE
Hint

Test the boundary itself: should exactly 135000 get the bonus?

Question 14

Question 14 of 15 · Coding

Debug it: average_sale(values) crashes with a runtime error when a salesperson has no sales. Make it return 0 for an empty list.

Open the IDE
Hint

Dividing by len([]) is dividing by zero. Check for an empty list first.

Question 15

Question 15 of 15 · Coding

Debug it: an employee ID must be exactly 8 digits. valid_id(emp_id) only checks the length, so "1234567a" gets through. Fix it.

Open the IDE
Hint

Strings have an isdigit() method.