4. The sum of scores. (random sample !)


#1

Hi,
I'm confused..
My code works for the given grades, but apparently the website want me to do it with their random ones..
So it doesn't work.. At start I made the simplyest solution, (like below) but that's not what they want either ofc, so I did add all they ask, still not the solution; I don't see what I have to do in fact.. Linked a printscreen of my current code.

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
total = 0
    for grade in grades:
        total += grade
    return total

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def grades_sum(scores):
    total = 0
    for notes in grades:
        total += notes
    return total
grades_sum(scores)
print total

Oops, try again. It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned 1045.5 instead of 37 for grades_sum([37])


#2

the exercise expects a function. To verify your function works correctly, its called with different (random) lists.

So you need a function. I can't open the screenshot, or only really small


#3

oh, well, sorry, i'll put it here;

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def grades_sum(scores):
    total = 0
    for notes in grades:
        total += notes
    return total
grades_sum(scores)
print total

Oops, try again. It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned 1045.5 instead of 37 for grades_sum([37])


#4

so if i call the function with[37] as argument:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
# function declaration with scores as parameter
def grades_sum(scores):
    total = 0
    for notes in grades:
        total += notes
    return total
grades_sum(scores)
grades_sum([37])

it wouldn't work, because you are looping over grades.

a argument ([37]) gets copied into the function parameter (scores), so if you want your function to work for different lists which you supply when calling the function, you shouldn't loop over grades, because that is one fixed list, and not the list you supply as argument

Its important to understand here how function parameters and arguments work


#5

So, "scores" and "grades" are indeed the distinction between the given grades and the random ones the site want me to sum isn't it ? Yeah it works now, and I see the point yep, just feel scrued because i'm lost, never know if they want me to use an input, the given arguments or a random one that come from nowhere btw..
But well, thanks much Stetim, agin.. =3


#6

grades is a list.

score is the function parameter, holding whatever argument you supply when calling the function


#7

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