Skip to content Skip to sidebar Skip to footer

How To Not Hardcode Function In This Example

The following links contain 2 csv files that the function should pass through grades_1e_2a grades_2e_4a However my function is only able to pass the 2nd linked file, as it is hardc

Solution 1:

As both sets of your data start the same place the following works for idx,i in enumerate(range(4,len(grades_list))):

This should fulfill all requirements that Im aware of up to this point

defclass_avg(open_file):
    '''(file) -> list of float
    Return a list of assignment averages for the entire class given the open
    class file. The returned list should contain assignment averages in the
    order listed in the given file.  For example, if there are 3 assignments
    per student, the returned list should 3 floats representing the 3 averages.
    '''
    marks = None
    avgs = []
    for line in open_file:
        grades_list = line.strip().split(',')
        if marks isNone:
            marks = []
            for i inrange(len(grades_list) -4):
                marks.append([])
        for idx,i inenumerate(range(4,len(grades_list))):
            marks[idx].append(int(grades_list[i]))
    for mark in marks:
        avgs.append(float(sum(mark)/(len(mark))))
    return avgs

Solution 2:

Try like this:

def class_avg(open_file, start=4, end=8):
...
...
    for idx,i in enumerate(range(start, end)):

Solution 3:

Try using this:

for idx,i in enumerate(range(4,len(grades_list))):
         marks[idx].append(int(grades_list[i]))

Considering you know how many assignments are there and initialized the marks list accordingly.

Post a Comment for "How To Not Hardcode Function In This Example"