8.14. Write Code Questions¶
The following data file
uspoll.txt is used in some of the questions in this section. The data
City, State: PM10: PM2.5. PM 10 is annual mean amounts of particulate pollution that’s over 2.5 and less than 10
micrometers in diameter, and PM2.5 is the annual mean amounts of particulate pollution
that’s 2.5 micrometers or less in diameter. Click show to see all of the data
and hide to put it away.
Fix errors in the code below so that the code runs correctly and prints the pollution for all cities that start with the letter A.
It was missing a close double quote after the r on line 1. It was missing a colon at the end of line 3. It was missing a comma or plus sign between the values on line 7.
Fix the errors in the code below so that it returns the average PM 2.5 value for the passed state (using the two letter abbreviation). Each line of the file is in the format
City, State: PM10: PM2.5. For example, the first line is “Aberdeen, SD :13 :8”. It should print 11.33333333333333.
The code below prints all the lines that have a city that starts with an “A”. Change it so that it prints out all lines that have a city that starts with “A” or “B”.
Fix the indention below for the code to correctly find and print the lowest 2.5 value and city. It should print
Smallest PM 2.5 4.0 in Bellingham, WA.
The following sample file called
studentdata.txtcontains one line for each student in an imaginary class. The students name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.
joe 10 15 20 30 40 bill 23 16 19 22 sue 8 22 17 14 32 17 24 21 2 9 11 17 grace 12 28 21 45 26 10 john 14 32 25 16 89
Using the text file
studentdata.txtwrite a program that prints out the names of students that have six or more quiz scores.
The following file contains a set of emotions that will be used in the next question.
Create a list called
j_emotionsthat contains every word in
emotion_words.txtthat begins with the letter “j”. After the code executes
['joyous', 'jittery', 'jumpy']
The rest of the questions gather their data from the file
stocks.txt, which shows the monthly Dow
Jones averages from 1989 to 2001. The data is in the order: Date, Open, High, Low, and Close. The first
3-Dec-01,9848.93,10220.78,9651.87,10021.57. The Date is in the format
day-month-yy. The month
is the first three letters of the month.
Complete the code at the
#so that it prints out the date with the biggest loss from open to close. Each line has: Date, Open, High, Low, and Close. It should print
3-Aug-98 loss 1329.030000000001.
Fix the errors below so that the
pointGainfunction returns a list of all the dates where the Dow Jones gained more than 300 points from open to close. Each line has: Date, Open, High, Low, and Close. The Date is in the format
day-month-yy. There should be 22 dates.
The code below prints all the dates and high price for dates that occur on the first day of the month (i.e. January 1, February 1…). Each line has: Date, Open, High, Low, and Close. Change it so that it prints the date and low price for all the dates that occur in June.
Write a function
avg_month_close(lines, month)that takes the
linesfrom the stocks file in a list and the abbreviation for a
month(i.e. “Jan”, “Feb”) as parameters and returns the average value of the closing prices during that month for all the years in the file. Each line has: Date, Open, High, Low, and Close. The Date is in the format
day-month-yy. The month is just the first three letters of the month.