In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.
The program below works with the same spelling test results as the previous
activity, stored in a 2D array called table_data. Run it once before you
start.
table_data = [
["Bob", 5, 9, 10, 7],
["Mary", 4, 10, 3, 6],
["James", 7, 6, 4, 8]
]
num_students = len(table_data)
for row in range(0, num_students):
student_name = table_data[row][0]
total_score = 0
for col in range(1, 5):
current_score = table_data[row][col]
total_score = total_score + current_score
print(student_name + " scored a total of " + str(total_score))
What data type is stored at index 0 of each inner array within
table_data?
A string: "Bob", "Mary" and "James" are all pieces of text.
Every other item on each row is an integer, a whole number. That is why the
program handles column 0 separately from the others: the name is stored in
student_name and only the columns from 1 onwards are added up. Adding
"Bob" to a number would stop the program with a TypeError.
What specific integer value does the len(table_data) function return on
line 7?
3.
table_data is a list of three rows, and len counts the items in the outer
list, so it counts rows. It does not count the fifteen items inside them: that
would take len of each row, which is 5, times the number of rows.
What is the purpose of the inner count controlled loop starting on line 13?
It adds up one student’s four test scores.
col takes the values 1, 2, 3 and 4, which are the four columns that hold
marks. Column 0, the name, is skipped because the range starts at 1. Each time
round, the mark in that column is added to total_score, so after four
repetitions total_score holds that student’s total.
What will be the value of the total_score variable immediately after the
first full iteration of the outer loop completes?
31.
On the first iteration row is 0, which is Bob’s row. The inner loop adds up
his four marks: 5 + 9 + 10 + 7 = 31. The print on line 17 then shows
Bob scored a total of 31.
You can check it by adding print(total_score) straight after the inner loop,
indented to the same level as the print on line 17.
How does the value of the outer loop counter (row) directly affect which
data is assigned to the student_name variable?
row is used as the first index in table_data[row][0], so it chooses which
row the name is taken from. The second index is always 0, the name column.
When row is 0 the name is Bob, when it is 1 it is Mary, and when it is 2
it is James. The same row is also used inside the inner loop, which is why
the marks that get added up always belong to the student whose name was just
picked out.
What logical error would occur in the final output if total_score = 0 was
moved to line 8, completely outside of the outer loop?
The totals would carry on from one student to the next instead of starting again at 0 for each student. Bob’s would still be right, but every later total would include all the marks before it:
Bob scored a total of 31
Mary scored a total of 54
James scored a total of 79
Mary really scored 23 and James 25. The program still runs without an error message, which is what makes it a logical error: the code is valid Python, it just calculates the wrong thing. Try it: move the line and run the program again.
Taking all the code together, what real-world problem is this algorithm designed to solve?
Working out each student’s total score across all of their spelling tests, and reporting it by name.
It is the kind of thing a teacher’s markbook does. Because the outer loop uses
len(table_data), it would keep working for a whole class if more rows were
added, without changing any of the loop code.
Make sure that you check for the following things:
table_data[row][col].str() before they are joined onto text with a +.