can anyone help me with this ?? No errors but not doing what I want !!

+1 Daniel O'Reilly · November 21, 2015
Anyone know why this won't return the averages for me ?? All I need this to do is print the averages out for me but it doesn't print anything and i'm not getting any errors ?? 

import csv

def getDataList(fileName):
dataFile = open(fileName, "r")
dataList = []
for line in dataFile:
# strip end-of-line, split on commas, and append items to list
dataList.append(line.strip().split(','))
return dataList

# find monthly average
def getMonthlyAverage(dataList):
mnth = 11 # set to 11 because this is the first month read
vol_close = 0
vol_sum = 0
monthlyAverageList = []

for rows in dataList:
year = int(rows['Date'][0])
month = int(rows['Date'][1])
vol = rows['Volume']
adj_close = rows['Adj Close']
if month != mnth:
average = vol_close / vol_sum
my_tuple = (average, month, year)
monthlyAverageList.append(my_tuple) # adds the avg_tuple to the end of the list
vol_close = 0 # resets the motnh to 0
vol_sum = 0 # resets the month to 0


vol_close += adj_close * vol # ((v1*c1)+(v2*c2)...+(vn*cn))
vol_sum += vol #(v1+v2...+vn)
mnth = month
return monthlyAverageList

def printInfo(average):
average.sort() # sorts the list of tuples
average.reverse() # reverses the list of tuples
print('\tDate\t\tAverage Price')
for row in average: # loops through each item of average
print("\t{:.2f}".format(row[2], row[1], row[0]))


fileName = 'googlePrices.csv'
dataList = getDataList(fileName)
dataList.pop(0)
average = getMonthlyAverage(dataList)
printInfo(average)
#print monthlyAverageList

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · November 22, 2015
Check tabs and spacing to make sure everything is indented the way you want it.

Indentation is critical in python. I don't know if it is because of what you posted, but the if and for loop statements look like they are not indented correctly.

For code you want to run in a loop, the code to be run within the for loop should be indented.
  • 1

Python

107,136 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator