Python Forum
Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it?
#1
url = 'http://nancymcohen.com/csci133/cpiai.txt'
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))
In this task, we are going to write a program test6.py that reports Consumer Price Index and its average value for any year selected by the user. Additionally, the user will be able to choose the months of the year they want to display. The program will have to use a list comprehension for transforming the list of months into the list of corresponding CPI values.


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.
Reply
#2
I'm not sure when 11:59 PM is, so it might be too late for this particular question, but...
My guess is that the reason you're not getting a reply is not asking a question.

Quote:can you tell me where I went wrong and help fix it?
This might look like a question, but it's not a very good one.
How are we supposed to know what the problem is?

You haven't included a traceback, so we don't know what exception you get.
If you don't get an exception, you should describe how the output you get is different from the one you want.

Sure, we could figure these things out by running your code, but you want to make it as easy as possible for people to help you.

Btw, you're missing a ] on line 30, and your indentation after that is wrong.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,381 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 1,558 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,234 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,707 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Something is Wrong with my code susmith552 4 3,047 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,388 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 7,920 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Why is this code wrong? Lemmy 4 5,185 Apr-05-2018, 03:46 PM
Last Post: Lemmy
  What's wrong with my code and visuals for python? beginnercoder04 2 2,826 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04
  whats wrong with my code? syntax errors r6lay 5 6,506 Mar-16-2017, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020