 # 5. Computing the average

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

total=0
total = total + i

total=0
total = total + i
return new_total
``````

My code is getting the error “Oops, try again. grades_average([3, 8, 5, 7]) returned 5 instead of the expected: 5.75”

Anyone?

try this for the average

``````def grades_average(grades):
total=0
total = total + i
new_total = float(total) / len(grades) # you have to put this outside the for loop
return new_total

``````
3 Likes

This function is right, but can be simplified, as follows:

``````def grades_sum(list):
total = 0
for i in list:
total += i

``````

This function repeats code, rather than using the utility function above. We may simplify it as follows:

``````def grades_average(list):
return float(total) / len(list)

``````
1 Like

Supplemental:

Python has some built in methods that further simplifies the above. Consider the following:

``````def grades_sum(scores):
return sum(scores) / 1.0  # converts sum to a float

``````
4 Likes

1 Like

Another short one:

I didn’t want to rewrite the function, this help out a lot, I got error messages when I first tried this method out, only because I forgot a “s” when I called the function grades_average. That messed with me for a good 10 minutes. Thanky thanky mtf.

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

total = 0
total += i

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

total = 0
for x in scores:
total += x

# i like to work with variables

av = i/ln
return av

something wrong happen with my grades_average. It running well at the console, but they say

``````Oops, try again.

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

total = 0
total += g

now everything works like a charm… 