Sum() function (Just weight and see)


#1

My code works fine

def get_average(student):
homework = average(student[‘homework’])
quizzes = average(student[‘quizzes’])
tests = average(student[‘tests’])
homework = homework * 0.1
quizzes = quizzes * 0.3
tests = tests * 0.6
return homework + quizzes + tests

But when I enter the return as:

def get_average(student):
homework = average(student[‘homework’])
quizzes = average(student[‘quizzes’])
tests = average(student[‘tests’])
homework = homework * 0.1
quizzes = quizzes * 0.3
tests = tests * 0.6
return sum(homework, quizzes, tests)

it gives an error. Any idea as to why?


#2

sum() is a list function.

    return sum([homework, quizzes, tests])

#3

Ahh, thank you so much! You’re brilliant! =D


#4

Just some silliness…

def get_average(student):
    keys = {
        'homework': 0.1,
        'quizzes': 0.3,
        'tests': 0.6
    }
    grades = []
    for key in keys:
        grades.append(average(student[key]) * keys[key])
    return sum(grades)

or,

def get_average(student):
    keys = {
        'homework': 0.1,
        'quizzes': 0.3,
        'tests': 0.6
    }
    return sum([average(student[key]) * keys[key] for key in keys])

#5

I’m a complete noob at coding, but I love playing around with it. Never imagined it could be done in these two ways as well. That’s amazing. 0_0


#6

By the end of this course you will be able to come up with similar code on your own. Just bookmark this page and keep moving forward. Technically, the first example is within the scope of what’s been learned thus far in the track, but the second is not covered until the unit on Advanced Topics.


#7

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