mercredi 29 juillet 2015

Python local variable modified but isn't shown at the end

a = 'file'
b = 'file'
c = 4
d = 4
e = 5
f = 6

def sim(a,b,c,d,e,f):
    s = 0
    if( a == b):
        s+=1
        print(s)
    if( c==d ):
        s+=1
        print(s)
    if (e == d):
        s+=1
        print(s)
    score = s/3
    return score

>>> sim(a,b,c,d,e,f)

Output for the above is:

1
2
0

This means although s is updated at first if clauses, when it is accessed in score variable, it has the old value of s, which is 0. Can someone please explain how to correct this? Why doesn't the correct value of s is not shown to score variable

Aucun commentaire:

Enregistrer un commentaire