Dec-08-2020, 07:23 PM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
file = urllib.request.urlopen(url) lines = file .readlines() file .close() cpi = {} for line in lines: items = line.decode().split() if len (items) > 0 and items[ 0 ].isdigit(): cpi[ int (items[ 0 ])] = [ float (item) for item in items[ 1 : 13 ]] shelf = shelve. open ( 'cpi' ) shelf[ 'cpi' ] = cpi shelf.close() query = input ( "Enter year number and list of month numbers separated by spaces: " ) x = query.split() if len (x) = = 1 : print (cpi[ int (query)]) numbers = cpi[ int (query)] size = len (numbers) total = 0 for month in range ( 0 , 12 ): total = total + numbers[month] average = total / size print ( "The average CPI for this year is: " + str (average)) if len (x) > 1 : year = (x[ 1 ]) month = (x[ 2 : 13 ]) monthValues = [cpi[ int (year)] for month in x : print (cpi[ int (year)][ int (month)]) numbers = cpi[ int (year)][ int (month)] size = len (numbers) total = 0 for month in range ( 0 , 12 ): total = total + numbers[month] average = total / size print ( "The average CPI for these months in this year is: " + str (average)) |
output should look like:
Enter query: 1950
[23.5, 23.5, 23.6, 23.6, 23.7, 23.8, 24.1, 24.3, 24.4, 24.6, 24.7, 25.0] 24.066666666666666
Enter query: 1950 5 6 (May and June of 1950)
[23.7, 23.8] 23.75
Enter query: 1950 1 3 5 7 (January, March, May, and July of 1950)
[23.5, 23.6, 23.7, 24.1] 23.725
Enter query:
Step-by-step implementation:
1. Use urllib.request to download CPI data from http://nancymcohen.com/csci133/cpiai.txt.
2. Provide a user interface to look up the CPI values for any year. The program should read a year number from the keyboard and print out the list of CPI values for that year. After that, it should print out the average CPI for that year (by computing the average of the reported list).
output should look like:
Enter query: 1950
[23.5, 23.5, 23.6, 23.6, 23.7, 23.8, 24.1, 24.3, 24.4, 24.6, 24.7, 25.0] 24.066666666666666
Enter query: 2000
[168.8, 169.8, 171.2, 171.3, 171.5, 172.4, 172.8, 172.8, 173.7, 174.0, 174.1, 174.0] 172.19999999999996
Enter query:
3. Enhance the program by allowing the user to specify the list of months they want to see. A valid query may in addition to the year number contain a list of month numbers separated by spaces.
For example, 1950 1 3 5 7 requests the data for January, March, May, and July of 1950. If the months are not specified, report the full year. The average should be computed only for the reported months.
output should look like:
Enter query: 1950
[23.5, 23.5, 23.6, 23.6, 23.7, 23.8, 24.1, 24.3, 24.4, 24.6, 24.7, 25.0] 24.066666666666666
Enter query: 1950 1 3 5 7
[23.5, 23.6, 23.7, 24.1] 23.725
Enter query: 1950 5 6
[23.7, 23.8] 23.75
Enter query:
(To find the list of months requested by the user, you will have to split the user input into a list of strings. If the length of the list is equal to 1, it contains only a year number and you have to report the full year. Otherwise, the remaining elements of the list are the month numbers. Use slicing to extract these numbers.)
4. Finally, edit your program to use a list comprehension when transforming the list of month numbers into the list of corresponding CPI values.
Don’t worry what happens if the user enters invalid year or invalid month numbers.