The sum of the scores


#1



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

well i think my code is perfect yet it shows me error saying did you forget to print the sum
return s should print the sum automatically but why isnt it doing it?
although when i added a print s command above the return all tests were passed


#2

as Its saying!
print it!

hint**

to print a number..

print 6 #it will print 6

another thing to know,,,
when function returns a value ,It just handles you a value ,It does not prints for you.
You can catch this value inside a variable and print it or you can directly print it

for ex..

def Anyfunction():
    return 5

print Anyfunction() # will print 5

#or..

x = Anyfunction()

print x # will print 5

when you use print...

def Anyfunction():
    print 5

Anyfunction() # will print 5

But you wont be able to use the value with print but just print it,return makes function more powerful ,It can be used like a variable!


#3

what is the difference between a return and a print statement?
both are returning values right?


#4

I explained in the above post,do reply if you get that concept or not


#5

okay i get it now to use the function further as a variable i have to use return and the print is for printing the value
but in the code i mentioned isn't the return s printing s ?


#6

as i have seen in earlier programs that the return command has printed the results where there was no need for the print statement
in any function if an integer value has to be returned i always used return 'x' command and it printed that on the console!!


#7

Nope,It just returning the value.

Think return as a employee of a function.
That only takes processed value from function and just returns it.
Then its work ends.

It does not print anything.


#8

but what about this?


#9

It was actually CC's interpreter that had printed values for you.


#10

okay now i am confused.
return never printed my value on the console???


#11

Never!
It was done by CodeCademy Interpreter (to show the result)

one good article to check out...


#12

argh..they should have told me and i could have change my code for future programs

anyways you have been great help
thanks for giving it your time


#13

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