Computing the average


#1

https://www.codecademy.com/en/courses/python-intermediate-en-7mgOa/1/2?curriculum_id=4f89dab3d788890003000096

The correct average (291.0) is printed, but I receive this error message: "Oops, try again. You did not print the correct average. Double-check your code and make sure you print the average!"

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
print len(grades)

#################################################################

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


def grades_average(grades):
    added = grades_sum(grades)
    avg = float((added / len(grades)))
    return avg

print grades_average(grades)

#2

here:

avg = float((added / len(grades)))

you divide an integer by an integer, python will floor the number (round down to a integer)

then you cast to float, but then the damage has already been done, the division itself should involve a floating number


#3

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