A Python program is designed to read weekly rainfall data from a text file, compute the total rainfall for each week, and then print the overall grand total.
Figure 1 shows the contents of the text file Rainfall.txt.
Figure 1: Rainfall.txt
12,15,8,14,21,10,20
22,18,25,30,15,20,20
35,40,25,30,20,15,15
42,38,50,25,25,20,20
Figure 2 shows the buggy Python code currently saved as Q05c.py.
Figure 2: Buggy Python Code (Q05c.py)
file = open("Rainfall.txt", "r")
grand_total = 0
for line in file:
values = line.strip().split(",")
week_total = 0
for val in values:
week_total = week_total + val
print(week_total)
grand_total = grand_total + week_total
print("Grand total: " + grand_total)
file.close()
Figure 3 shows the intended output of the program.
Figure 3: Intended output
100
150
180
220
Grand total: 650
Amend the Python code in Figure 2 so that it runs without runtime errors and produces the correct output shown in Figure 3.