4.the sum of scores hope you guys can help me


#1

I certainly don't know what's the problem with my code.

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

the computer returns me this:UnboundLocalError: local variable 'result' referenced before assignment


#2

you should place this line inside function definition, so first line after def grades_sum(scores):


#3

what's the difference of the code's meaning between them?


#4

it is a big difference and it refers to variable scopes. when you define a function (def) the variables you use in that function are only 'visible' inside that function - fe. in your code, if you add new line at the bottom:
print a
you will get error : NameError: name 'a' is not defined because you use variable a only in def function . if you place same print inside function (but after a is mentioned) it will print correctly.

same is with your result variable. the one defined in second row
result = 0 is outside function (in main code) so your function will not 'see' it. you need to define it inside the function so this can work:
result += a which is a compound statement and actually means: add a with result and store the outcome into result, so to be able to add a with result, result needs to be defined ... and it is not as it is outside the function so your function does not see it.


#5

Which you mean is that i can't use a variable defined outside the function in the function,am i right?


#6

technically you can, but you'd better not to as it may cause unpredictable results like in your code :slight_smile: the basic way (and good for now) is this: you pass variables into the function in the parameters (above scores is a parameter from the outside), in the return statement you move them outside function (your return result will move result outside the function).
so for your question - you can use variable defined outside in your function, but here it is not necessary as you can also define it inside the function (which is shorter and faster for this excercise).


#7

It makes me a little confused.You say that i can use variable defined outside in my function.Can you give me some examples?


#8

one example with passing 2 numbers into the function:

def adding(one_number,two_number):
    sum = one_number + two_number
    return sum

# this is outside function
x = 5 
y = 10
print adding(x,y)  # you pass x and y in this line into the function

#9

and example with numbers inside function

def adding(): 
    # this is inside the function 
    x = 5 
    y = 10 
    sum = x+y 
    return sum

# this is outside function

print adding()  # you do not pass anything here, you know that numbers are inside function

#10

Oh,I think i get it.I can type like this:

x = 5 
y = 10
print adding(x,y)

but can't type like this:

sum=0
def adding(numbers):
    for i in numbers:
    sum +=1
    return sum

#11

it is just a matter where you have your sum =0: inside or outside the function :slight_smile: and it means quite different things.


#12

sum=0
def adding(numbers):
    for i in numbers:
    sum +=1
    return sum

Is this code making the same mistake as my code before?


#13

yes, same mistake because sum=0 is outside the function (outside can be before or after, does not matter - still it is outside)


#19