Comments that explain and justify data and code structures

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Comments that explain and justify data and code structures

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which comment best justifies a choice of data structure?
A # this is an array
B # array used because the size is fixed at 30 students
2. Which comment best describes the purpose of the variable totalMarks?
A # totalMarks holds the running sum of all test marks for the class average
B # totalMarks equals 0
3. Which comment avoids simply repeating the code? count = count + 1
A # count tracks how many students passed
B # add 1 to count
4. Which comment best explains why a loop is used?
A # loop through every mark so each one is added to the total
B # for loop
5. Which comment best justifies using a record for a student?
A # student record
B # record used because name, age and mark are different data types

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1# total holds the running sum of all marks A# loop through every mark to find the highest for mark in marks:
2# list used because the number of students can change BA comment describing a variable's purpose: what the variable is for
3A comment that explains why a loop is used and what it achieves CThe comment only repeats what the code does and does not explain what count is for. Better: # count tracks how many students passed
4A comment that justifies using a record to group related data of different types DThe comment names the structure but gives no reason for choosing it. Better: # array used because the size is fixed at 30 students
5# set count to 0 count = 0 E# record used to keep one student's name, age and mark together student = {"name": "Ava", "age": 16, "mark": 88}
6# this is an array scores = [0] * 30 FA comment justifying the choice of data structure by giving the reason it was chosen

Definitions

# array used because size is fixed

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_ total of all marks, used to find the average total = 0
2.
# loop runs once for each of the 30 students ___ i in range(30): marks.append(0)
3.
# dictionary used so marks are found by name marks = __
4.
# repeat until a valid mark is entered _____ mark < 0 or mark > 100: mark = int(input("Mark: "))
5.
# list used because the size is fixed at 30 names = [""] _ 30

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A # fixed size: 30 students
B marks = [0] * 30
C total = 0
D // total: sum used for average
E for m in marks: total += m
Question 2
A # array: 5 days, size fixed
B temps = [0] * 7
C for d in range(7):
D temps[d] = d + 20
Question 3
A students = []
B students.append("Mia")
C # array used: size is fixed
D print(len(students))
Question 4
A tests = 5
B for i in range(tests):
C # for i in range(tests)
D print(i + 1)
Question 5
A mark = 72
B # pass mark is 50 or more
C if mark >= 60:
D print("Pass")

Answer Key

Comments that explain and justify data and code structures
Section A: Quick Questions
Q1
B — # array used because the size is fixed at 30 students
Q2
A — # totalMarks holds the running sum of all test marks for the class average
Q3
A — # count tracks how many students passed
Q4
A — # loop through every mark so each one is added to the total
Q5
B — # record used because name, age and mark are different data types
Section B: Match the Pairs
Q1
# total holds the running sum of all marks → A comment describing a variable's purpose: what the variable is for
Q2
# list used because the number of students can change → A comment justifying the choice of data structure by giving the reason it was chosen
Q3
A comment that explains why a loop is used and what it achieves → # loop through every mark to find the highest for mark in marks:
Q4
A comment that justifies using a record to group related data of different types → # record used to keep one student's name, age and mark together student = {"name": "Ava", "age": 16, "mark": 88}
Q5
# set count to 0 count = 0 → The comment only repeats what the code does and does not explain what count is for. Better: # count tracks how many students passed
Q6
# this is an array scores = [0] * 30 → The comment names the structure but gives no reason for choosing it. Better: # array used because the size is fixed at 30 students
Section C: Definitions
Q1
# array used because size is fixed — A comment that explains what a variable, array, record or algorithm is for and why it was chosen, rather than just repeating what the code does.
Section D: Fill in the Blanks
Q1
# total of all marks, used to find the average total = 0
Q2
# loop runs once for each of the 30 students for i in range(30): marks.append(0)
Q3
# dictionary used so marks are found by name marks = {}
Q4
# repeat until a valid mark is entered while mark < 0 or mark > 100: mark = int(input("Mark: "))
Q5
# list used because the size is fixed at 30 names = [""] * 30
Section E: Spot the Error
Q1
D — Python comments start with #, not //. The // line is invalid code and causes a syntax error. It should be '# total: sum used for average'.
Q2
A — The comment gives the wrong reason for the array's size. The code creates 7 elements, so the comment must say 7 days. A comment that contradicts the code misleads the next programmer.
Q3
C — The comment justifies the structure with the wrong reason. The code uses append(), so the size grows. It should say the list was chosen because its size can change.
Q4
C — This comment only repeats the code. A good comment explains why the loop exists, such as '# loop: one pass per test'.
Q5
C — The comment says the pass mark is 50, but the condition tests for 60. The code and its explanation must agree, so the condition should be mark >= 50.