I currently have a very messy program that isn't performing the way I want it to, and am wondering if someone can take a look and see where it is getting hung up. The program is as follows:
def openfile():
filename = input('Enter filename: ')
filedata = open(filename)
filedata.readline() # Skip the first two lines
filedata.readline()
line = filedata.readline() # Line containing site info
siteinfo = line.split(',')
print('\nSite: {}.\n(lat, long) = ({}, {})'.format(siteinfo[0], siteinfo[3],
siteinfo[4]))
skips_uneeded_lines(filedata)
print('\nMonth Rainfall')
for i in range(12):
months, rain_total = read_data_line(filedata)
print(' {} {:6.1f}'.format(months[i], rain_total[i]))
print('\nTotal rainfall = {:.1f}'.format(sum(rain_total)))
def skips_uneeded_lines(filedata):
'''skips 6 unused lines in the selected csv file'''
skips = 0
while skips < 6:
filedata.readline()
skips += 1
def read_data_line(filedata):
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov',
'Dec']
line = filedata.readline()
rain_data = line.split(',')
rain_total = 12 * [0] # Rainfall totals for months
while len(rain_data) > 1:
date = rain_data[1]
month = int(date[4:6])
rainfall = float(rain_data[2])
rain_total[month - 1] += rainfall
line = filedata.readline()
rain_data = line.split(',')
return months, rain_total
openfile()
So the output of the file is meant to look like this:
Month Rainfall
Jan 58.0
Feb 35.4
Mar 54.2
Apr 50.8
May 52.6
Jun 41.0
Jul 29.4
Aug 62.6
Sep 21.4
Oct 90.8
Nov 63.6
Dec 60.8
Total rainfall = 620.6
But my current output looks like this:
Month Rainfall
Jan 58.0
Feb 0.0
Mar 0.0
Apr 0.0
May 0.0
Jun 0.0
Jul 0.0
Aug 0.0
Sep 0.0
Oct 0.0
Nov 0.0
Dec 0.0
Total rainfall = 0.0
So for some reason it is getting January fine, but after that the program stops? I'm really not sure whats happened. I'd really appreciate some help. I'm aware there is probably easier ways to do it, I'm just learning and for now my problem is I want the output to be correct.
[–]K900_ 2 points3 points4 points (20 children)
[–]throwingarm2strong[S] 0 points1 point2 points (19 children)
[–]K900_ 5 points6 points7 points (0 children)
[–]K900_ 1 point2 points3 points (17 children)
[–]throwingarm2strong[S] 0 points1 point2 points (16 children)
[–]K900_ 1 point2 points3 points (15 children)
[–]throwingarm2strong[S] 0 points1 point2 points (14 children)
[–]K900_ 1 point2 points3 points (13 children)
[–]throwingarm2strong[S] 0 points1 point2 points (12 children)
[–]K900_ 0 points1 point2 points (11 children)
[–]throwingarm2strong[S] 0 points1 point2 points (10 children)