This time, we would like to get the average pollution level. To do that, we need to take every PM2.5 value and add them all together. As we do this, we need to keep a count of how many values we have seen, so we can compute an average.
This program will calculate the average PM2.5 pollution level in the US data. Arrange the blocks in the right order and indent them correctly. You will use all of them.
# read all the lines
inFile = open("uspoll.txt")
data = inFile.read().splitlines()
inFile.close()
# initialize the variables
total25 = 0
count = 0
---
# loop through the lines
for line in data:
---
# split at :
values = line.split(":")
---
# get the PM 2.5 pollution
new25 = int(values[2])
---
# add to the total and count
total25 = total25 + new25
count = count + 1
---
# print the average PM 2.5 value
print("Average PM 2.5 value is ", total25 / count)
Now let’s do the same thing for the PM10 values. This program is mostly done. There are a few lines that need to still be written, they are all marked with a TODO comment. Finish up the program so you can find the average PM10 level.