how to create a global array in Python -


i want create global array these functions fib() , shoot() can share it. here code:

global f f=[0]*1000 def fib(n):      f[0]=1  ## error here       f[1]=1     in xrange(2,n+1):         f[i]=f[i-1]+f[i-2]  def shoot(aliens):     ...     place use f[] here  fib(999) print shoot(line) 

however shows error.

traceback (most recent call last): file  line 56, in <module> fib(999) line 42, in fib f[0]=1 typeerror: 'file' object not support item assignment 

please help!

edit: comments below made me realise had "with open('somefile') f" in part of code not shown here. removed that, , now, it's working.

you overrode list f with:

with open(...) f: 

you can either:

  • rename list

  • change name of file (i.e, as myfile)

because happened, you're trying access list indexing, you're working file object. why typeerror: 'file' object not support item assignment


Comments