Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Average
#3
Not homework.  Wish I was back in school.  I have a New Years resolution to learn Python and use it at work whenever I can.  I deal with very large data sets and often the records exceed the 1M mark to use in Excel.  Any suggestions would be appreciated.
  
Here is the code I wrote.  Yes it's poor I know.

f = open("pricelist.txt", "r")

SSPList = {}

for line in f:
    entry = line.strip().split(",")
    PartNo = entry[0]
    Price = entry[1]
    AvgPrice = sum(Price)/len(SSPlist)
f.close()

print(SSPList)


and here is my error message:

Traceback (most recent call last):
  File "C:/Python34/Scripts/SSPTest.py", line 8, in <module>
    Price = entry[1]
IndexError: list index out of range

Ok, so I messed with it a little more, but I struggle with how to calculate the average.  Here is my new code and error message:

f = open("pricelist.csv", "r")

SSPList = {}

for line in f:
    entry = line.strip().split(",")
    PartNo = entry[0]
    print(PartNo)
    Price = entry[1]
    print(Price)
    AvgPrice = sum(Price)/len(SSPlist)
    SSPList[PartNo] = AvgPrice

f.close()

print(SSPList)



Error message:
>>> 
A
10
Traceback (most recent call last):
  File "C:\Python34\Scripts\SSPTest.py", line 11, in <module>
    AvgPrice = sum(Price)/len(SSPlist)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
Reply


Messages In This Thread
Simple Average - by ddoogles - Feb-02-2017, 05:40 PM
RE: Simple Average - by buran - Feb-02-2017, 05:45 PM
RE: Simple Average - by ddoogles - Feb-02-2017, 06:23 PM
RE: Simple Average - by buran - Feb-02-2017, 07:12 PM
RE: Simple Average - by wavic - Feb-02-2017, 07:49 PM
RE: Simple Average - by ddoogles - Feb-02-2017, 09:46 PM

Forum Jump:

User Panel Messages

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