The sum of scores: it return none - error alert -


#1

Hi. I'm with no best alternatives. I don't know what it's necessary to correct the code below:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def grades_sum(scores):
    total = 0.0
    for i in range(len(scores)):
        total = sum(float(i) for i in scores)
    print total
grades_sum(grades)

I would like to know why I'm receiving the error alert, "It returned None", when if i to put some values, in list format, like grades_sum([45, 32]) the function returns the correct sum: 77.0

Error alert:
Oops, try again. It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned None instead of 324 for grades_sum([73, 50, 44, 68, 89])


#2

@blogmaster36220

---------First-----------

We create a function called grades_sum which takes in argument scores. Function syntax looks like this:

def function_name(optional arguments):

-------Second-----------

We create total variable that we are going to return at the end of our function. It is going to store the sum of grades. For the moment, it will have the value of 0.

total = 0 # we set a variable called total

---------Third-----------

We loop inside of scores with the for loop. We are going to set the value of total variable to itself plus each score.

for score in scores: #we loop over scores array
    total +=score # we add score to total

-------Finally----------

Outside the for loop, return the value of total.

return total #we return total to the console

Hope this helped!


#3

oh! i don't believe! i spent at least 4 hours trying to figure out why the function was returning "none"...
thank you very much!