The Chief Examiner's report highlights that Paper 2 tests your ability to read, analyse and refine code. You will be asked to fix errors, rearrange code, fill in blanks, complete trace tables, and design full programs. Practice each skill below.
Question 1: Error Correction
8 marksThe following Python program is intended to convert a temperature from Celsius to Fahrenheit. However, it contains 4 errors (a mix of syntax and logic errors). For each error, identify the line and type the corrected version of that line.
1celsius = float(input("Enter temperature in Celsius: "))
2fahrenheit = (celsius * 9/5) + 32
3print("Result: " + farenheit + " degrees F")
4if celsius > 30
5 print("That is hot!")
6elif celsius > 20:
7 print("That is warm.")
8elif celsius = 20:
9 print("That is exactly 20 degrees.")
10else:
11 print("That is cool.")
Identify and correct each error:
Type the corrected version of line 3:
Type the corrected version of line 3 (fix both issues on this line):
Type the corrected version of line 4:
Type the corrected version of line 8:
- Error 1 - Misspelled variable:
farenheitshould befahrenheit[1 mark] for identifying, [1 mark] for correction - Error 2 - Missing str(): Need
str(fahrenheit)to concatenate with string [1 mark] for identifying, [1 mark] for correction - Error 3 - Missing colon:
if celsius > 30:requires a colon [1 mark] for identifying, [1 mark] for correction - Error 4 - Wrong operator:
=should be==for comparison [1 mark] for identifying, [1 mark] for correction
Corrected line 3: print("Result: " + str(fahrenheit) + " degrees F")
Corrected line 4: if celsius > 30:
Corrected line 8: elif celsius == 20:
Question 2: Code Rearrangement
6 marksThe following lines of code should form a working password checker program. The program asks the user for a password, gives them 3 attempts, and tells them whether they got it right or ran out of tries. Drag the lines into the correct order.
Available Code Blocks:
Build Your Program (drag lines here):
Correct order (1 mark per correctly placed line, up to 6 marks):
password = "secret123"[1 mark]attempts = 0[1 mark]while attempts < 3:[1 mark]guess = input("Enter password: ")[1 mark]if guess == password:[1 mark]print("Access granted!")[0 marks - free, part of structure]attempts = attempts + 1[1 mark]print("No attempts remaining.")[0 marks - free, closing statement]
Question 3: Complete the Code
6 marksThe program below is a shopping list manager. It allows the user to keep adding items until they type "done", then displays the complete list. Some lines are incomplete. Select the correct Python statement from each dropdown to complete the program.
shopping_list = []
item = ""
item = input("Enter an item (or 'done' to finish): ")
if item != "done":
print("Your shopping list:")
for i in range(len(shopping_list)):
print(str(i + 1) + ". " + shopping_list[i])
else:
print("Your shopping list is empty.")
- Blank 1:
while item != "done":— a while loop is needed because we do not know how many items the user will enter [2 marks] - Blank 2:
shopping_list.append(item)— append adds a single item to the end of a list. insert requires an index. add is for sets. extend is for adding multiple items. [2 marks] - Blank 3:
if len(shopping_list) > 0:— checks if the list has any items before displaying [2 marks]
Question 4: Trace Table
8 marksThe program below calculates the factorial of 5 (i.e. 5! = 5 × 4 × 3 × 2 × 1 = 120). Complete the trace table by filling in the values of each variable after each iteration of the loop.
1number = 5
2result = 1
3counter = 1
4while counter <= number:
5 result = result * counter
6 counter = counter + 1
7print(result)
Complete the trace table (fill in the blank cells):
| Iteration | counter (start) | counter <= number | result (after line 5) | counter (after line 6) |
|---|---|---|---|---|
| 1 | 1 | True | ||
| 2 | 2 | True | ||
| 3 | 3 | True | ||
| 4 | 4 | True | ||
| 5 | 5 | True | ||
| 6 | 6 | Loop ends — print(result) outputs 120 | ||
1 mark per correct cell (allowing for follow-through errors):
- Iteration 1: result = 1 (1*1), counter = 2
- Iteration 2: result = 2 (1*2), counter = 3
- Iteration 3: result = 6 (2*3), counter = 4
- Iteration 4: result = 24 (6*4), counter = 5
- Iteration 5: result = 120 (24*5), counter = 6
- Iteration 6: condition = False (6 <= 5 is False, so loop ends)
Note: Accept "false" (case insensitive) for the final condition. The printed output is 120.
Question 5: Code Improvement
6 marksThe program below works correctly but is poorly written. A good programmer would refactor it to be more readable, efficient, and maintainable. Read the code and answer the questions below.
1x = float(input("Enter rectangle length: "))
2y = float(input("Enter rectangle width: "))
3a = x * y
4print("Rectangle area: " + str(a))
5
6x2 = float(input("Enter triangle base: "))
7y2 = float(input("Enter triangle height: "))
8a2 = x2 * y2 * 0.5
9print("Triangle area: " + str(a2))
10
11x3 = float(input("Enter circle radius: "))
12a3 = x3 * x3 * 3.14159
13print("Circle area: " + str(a3))
Identify 3 improvements that should be made:
Improvement 1: How should the repetitive structure be improved?
Improvement 2: What should be done about the value 3.14159 on line 12?
import math and math.pi is even better.
Improvement 3: What is wrong with the variable names?
x, y, a tell us nothing about the purpose. Names like length, width, and rectangle_area make the code self-documenting and much easier for another programmer to understand.
- Improvement 1 (Functions): Use subroutines/functions to avoid repetitive code [1 mark] identification + [1 mark] explanation of why
- Improvement 2 (Constants): Use named constants or import math.pi instead of magic numbers [1 mark] identification + [1 mark] explanation of why
- Improvement 3 (Variable names): Use meaningful, descriptive variable names [1 mark] identification + [1 mark] explanation of why
Question 6: Program Design
12 marksScenario: A teacher wants a program that stores student names and their test scores. The program should allow the teacher to enter names and scores for the class, then calculate the average score and find the student with the highest score.
Your program must:
- Ask how many students are in the class
- Use a loop to collect each student's name and score
- Store the data in appropriate data structures (lists)
- Calculate and display the average score
- Find and display the student with the highest score
- Include appropriate input validation (score must be 0–100)
Write your solution in the editor below. Comment scaffolding has been provided to guide your structure.
# Student Score Tracker
# --------------------
# Step 1: Set up empty lists for names and scores
names = []
scores = []
# Step 2: Ask how many students are in the class
num_students = int(input("How many students? "))
# Step 3: Use a loop to collect each student's name and score
for i in range(num_students):
name = input("Enter student name: ")
# Add input validation for score (must be 0-100)
score = int(input("Enter score for " + name + ": "))
while score < 0 or score > 100:
print("Score must be between 0 and 100.")
score = int(input("Enter score for " + name + ": "))
# Store the data in the lists
names.append(name)
scores.append(score)
# Step 4: Calculate the average score
total = 0
for s in scores:
total = total + s
average = total / num_students
print("Average score: " + str(average))
# Step 5: Find the student with the highest score
highest = scores[0]
best_student = names[0]
for i in range(len(scores)):
if scores[i] > highest:
highest = scores[i]
best_student = names[i]
print("Highest score: " + best_student + " with " + str(highest))
Award marks as follows:
- Creating two lists (names and scores) [1 mark]
- Asking for the number of students [1 mark]
- Using a loop to collect names and scores [2 marks]
- Appending name and score to the correct lists [1 mark]
- Input validation: checking score is between 0 and 100 [2 marks]
- Calculating the total and average of scores [2 marks]
- Finding the highest score and corresponding student name [2 marks]
- Displaying results with appropriate messages [1 mark]
Note: Accept alternative valid approaches (e.g. using sum(), max(), dictionaries, etc.). The scaffolded code above is a model answer.
In the real exam, program design questions carry the most marks. Plan your answer before you write. Use comments to show the examiner your thinking even if your code has small errors. Marks are awarded for structure and logic, not just syntax.