Please i need help, dont seem to know what's wrong with my code


#1

This is the instruction

On line 3, define a function, grades_sum, that does the following:

Takes in a list of scores, scores
Computes the sum of the scores
Returns the computed sum.
Call the newly created grades_sum function with the list of grades and print the result.

This is my code:

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

def grades_sum(scores):
  total = 0
  for grade in grades:
    total += grade 
    return total
    print total  
print grades_sum(grades)

This is the error message am getting:

It looks like your grades_sum function doesn’t properly compute the sum of all the grades! It returned 100 instead of 363 for grades_sum([32, 13, 40, 98, 43, 44, 85, 8])


#2

you are computing the sum of grades ,not scores like told in the instructions

A function ends the moment a return keyword is reached, so your function ends in the first iteration of your loop, but to get the correct sum/total the loop need to make all its iterations, so return should be placed outside the loop


#3

The function parameter is scores. Within the function, access that variable instead of the global variable, grades.


#4

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