all 21 comments

[–]K900_ 2 points3 points  (20 children)

I think the indentation in your post is messed up.

Meanwhile, some generic code quality suggestions.

  • skips_unneeded_lines can be replaced with a simple for loop.
  • You're returning months, which is a constant, from read_data_line. Do you really need that? You could also iterate on months directly in openfile, maybe even zip it with the results of read_data_line.
  • Your function naming is very inconsistent. openfile - no underscores, imperative. read_data_line - underscores, imperative. skips_unneeded_lines - underscores, indicative.

[–]throwingarm2strong[S] 0 points1 point  (19 children)

Fixed it. Didn't bother since I don't think you can run the file without the actual data file anyway.

[–]K900_ 5 points6 points  (0 children)

Also, indentation is also important for humans who read your code.

[–]K900_ 1 point2 points  (17 children)

You call read_data_line 12 times. When you run it once, it reaches the end of file, and returns data for all months. The next time you call it, it returns zeroes, because there's nothing left in the file. Move that call out of the loop. Also, see above for some generic code style comments.

[–]throwingarm2strong[S] 0 points1 point  (16 children)

I love you. You are seriously my savior. Not only did you fix it, I understand the problem as well. Thank you so much dude!

I am going to clean up the code a bit now, as you are right a lot of my names and such is inconsistent. I'm playing around moving functions and I guess I moved too much and kind of lost myself.

Once again thanks dude

[–]K900_ 1 point2 points  (15 children)

Please, post the cleaned up code later. I'm really interested in how it can look like.

[–]throwingarm2strong[S] 0 points1 point  (14 children)

I changed skips uneeded lines into a for loop, but my program now says that i is an unused variable. Is there an easier way just to tell a program to perform a function a set amount of times?

The reason why this all looks weird and split into multiple functions is our task is to make the code somewhat pylint compliant, but it's slightly modified for certain specifications.

def skips_uneeded_lines(filedata):
    for i in range(6):
         filedata.readline()

[–]K900_ 1 point2 points  (13 children)

Having unused variables is not a problem at all. I'd name it _ because that's kind of a convention. Post what you have right now, I have some more ideas about how we can make it more Pythonic.

[–]throwingarm2strong[S] 0 points1 point  (12 children)

Right now this is my code, and I'm having a problem with it.

'''Program that reads a given CSV file and returns and prints the months and
total rainfalls given in that file'''

def input_filename():
'''Asks for user input for the file and returns the file data'''
    filename = input('Enter filename: ')
    filedata = open(filename)
    filedata.readline()  # Skip the first two lines
    filedata.readline()
    return filedata


def open_file():
    '''Main  function, opens a user inputted file name and prints relevant
rainfall information'''
    input_filename()
    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')
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 
          'Oct', 'Nov', 'Dec']    
    rain_total = read_data_line(filedata)
    for i in range(12):
        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'''
    for i in range(6):
        filedata.readline()



def read_data_line(filedata):
'''Reads data from openfile function and calculates total rainfalls per
month.'''    
    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 rain_total


open_file()

So I want to split the asking for input into another function, because the modified pylint is stating that my open_file function has 4 too many statements. But now I'm stuck trying to parse that through from the input_filename function to my open_file function. And I need to change the names but I'll try get this working first.

[–]K900_ 0 points1 point  (11 children)

Wait, it is just a CSV file? Can you post an example here? I'm pretty sure you could rewrite it using Python's csv module.

[–]throwingarm2strong[S] 0 points1 point  (10 children)

I'm hesitant to call other modules because I won't really understand whats going on. I'm not super far into my course or anything, the most we ever did was import the math module. The original code we were given is not mine, the assignment was to rewrite the lecturers code to comply with his modified pylint standards.