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.

 
1
# read all the lines
2
in_file = open("uspoll.txt","r")
3
lines = in_file.readlines()
4
in_file.close()
5
6
# initialize the variables
7
minCity = ''
8
min25 = 500
9
10
# loop through the lines
11
for line in lines:
12
13
    # split at :
14
    values = line.split(":")
15
16
    # get the PM 2.5 pollution
17
    new25 = float(values[2])
18
19
    # if current <  min
20
    if new25 < min25:
21
22
        # save new min info
23
        minCity = values[0]
24
        min25 = new25
25
26
# print the smallest PM 2.5 value
27
print("Smallest PM 2.5 ",min25," in ",minCity)
28

(min25)

csp-18-5-1: Will this code always work to find the minimum PM 2.5 value?



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.

14
 
1
in_file = open("uspoll.txt","r")
2
lines = in_file.readlines()
3
in_file.close()
4
5
minCity = ''
6
min10 = 500
7
for line in lines:
8
    values = line.split(":")
9
    # set the value for new10 to be the current PM 10 value
10
    if new10 < min10:
11
        # Save the minimum city and state
12
        # save the minimum PM 10 value
13
print("Smallest PM 10 value is ",min10," in ",minCity)
14

(min10_ex)

csp-18-5-2: Which of the following is true about the cities with the minimum PM 2.5 and PM 10 values?




csp-18-5-4: csp-18-5-3: 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 Check Me 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.
# split at :values = line.split(":")
# loop through the linesfor line in lines:
# print the smallest PM 2.5 valueprint("Smallest PM 2.5 ",min25," in ",minCity)
# read all the linesin_file = open("uspoll.txt","r")lines = in_file.readlines()in_file.close()# initialize the variablesminCity = ''min25 = 500
# get the PM 2.5 pollutionnew25 = float(values[2])
# save new min infominCity = values[0]min25 = new25
# if current <  minif new25 < min25:
Next Section - What’s the average PM 2.5 pollution?