mercredi 27 juillet 2016

Global variable in python function doesn't work for my code

This works.

a = [2,5,3,7,9,12]
def mean_list(x):
      b = 0
      for i in x:
            b=b+i
            c = b/len(x)
      return c
print(mean_list(a))

but this doesn't work, even though I have declared my list as global. Why?

def mean_list(x):
      global a
      a = [2,5,3,7,9,12]
      b = 0
      for i in x:
            b=b+i
            c = b/len(x)
      return c
print(mean_list(a))

Aucun commentaire:

Enregistrer un commentaire