Computing the Average - Why do they tell you to put wrong argument?


#1

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
  total = 0
  for score in scores: 
    total += score
  return total

print grades_sum(grades)

def grades_average(grades_input):    # THIS IS THE PART
    return grades_sum(grades) / float(len(grades_input))    # THIS IS THE PART

print grades_average(grades)
    

So normally in the instructions you want to follow what they say to put in for the argument especially when it is highlighted. Not this time though. The code above returns the correct answers but I don’t get the check mark. It says

grades_average([8, 6, 8, 3, 4, 6]) returned 174.25 instead of the expected: 5.833333333333333

I assume I am missing something as to how this site works in terms of instructions but my code printed

1045.5
80.4230769231

this is the same result if I change my code to get the green check mark to move on:

new code

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
  total = 0
  for score in scores: 
    total += score
  return total

print grades_sum(grades)


def grades_average(grades):   # THIS IS THE PART
    return grades_sum(grades) / float(len(grades))   # THIS IS THE PART

print grades_average(grades)

Why do I have to put ‘grades’ instead of the grades_input like they request when both yield the same result?


#2

If grades_input is the expected parameter, then use it inside the function so it is re-usable and not dedicated to the global object, grades.

Length is a counting number, so the float declaration should go on the numerator which could feasibly be a float.

return float(grades_sum(grades_input)) / len(grades_input)

An even better place to delcare the float would be in the dependency, grades_sum.

total = float(0)
...
return total

Now total will return as a float so does not need to be redeclared in grades_average.

return grades_sum(grades_input) / len(grades_input)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.