18.5. What’s the lowest PM 2.5 pollution?

If we can figure out the city with the maximum pollution, the city with the minimum pollution should be pretty easy to find as well.

This section uses the same data file that we have been using, but if you want to see all of the data click on the Show button below. Once it appears, you can hide it again by clicking on the Hide button.

Can you now write the code to get the city with the smallest PM 10 value? Change the lines that start with a comment (a #) below.

The following program prints the minimum PM 2.5 pollution found, but the code is mixed up. Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on <i>Check Me</i> to see if you are right. You will be told if any of the lines are in the wrong order or have the wrong indention.

        # read all the lines
inFile = open("uspoll.txt","r")
lines = inFile.readlines()
inFile.close()

# initialize the variables
minCity = ''
min25 = 500
---
# loop through the lines
for line in lines:
---
    # split at :
    values = line.split(":")
---
    # get the PM 2.5 pollution
    new25 = float(values[2])
---
    # if current <  min
    if new25 < min25:
---
        # save new min info
        minCity = values[0]
        min25 = new25
---
# print the smallest PM 2.5 value
print("Smallest PM 2.5 ",min25," in ",minCity)
        

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted of activities on this page