- 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:
- Test the code to identify defects
- Fix the errors so the code works correctly
- Document the process you followed
Types of Errors
1. Syntax Errors
These prevent the code from running at all. Python will show an error message immediately.
- Missing colons after if, while, for, def statements
- Incorrect indentation
- Misspelled keywords
- Unmatched brackets or quotes
2. Logic Errors
The code runs but produces incorrect results.
- Wrong operators (e.g., using + instead of =)
- Incorrect conditions (e.g., > instead of >=)
- Wrong calculations or formulas
- Incorrect variable assignments
3. Runtime Errors
The code runs but crashes during execution.
- Type errors (e.g., trying to add string to integer)
- Division by zero
- Index out of range
- File not found
Example: T-Learn Automotives Commission Calculator
Below is typical code you might encounter in the exam. It has several errors that need fixing.
Requirements
- Employee ID must be 8 digits long
- Commission is 5% of total sales value
- Bonus: £500 if 6 or more cars sold
- Additional bonus: 4% of total value if total ≥ £135,000
- Output summary to screen and text file
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:
- Describe each error you found
- Explain why it was wrong
- Show what you changed to fix it
- Reference the requirement it relates to
- Include screenshots or code snippets
Best Practices
- Fix one error at a time and test after each fix
- Keep the original code for reference
- Use meaningful test data
- Check all requirements are met
- Test multiple scenarios
- Follow Python conventions (PEP 8)