Printing out the fibonacci series
问题 I am trying to write a simple Python program. It's supposed to return a a closure that returns successive fibonacci numbers: def fibGen(): n_1 = 0 n_2 = 0 n = 1 def fib(): if n_1 ==0 and n_2 ==0: n_1 = 1 return n else: n = n_1 + n_2 n_2 = n_1 n_1 = n return n return fib f = fibGen() for i in range(0,10): print(f()) I get this error at run time: UnboundLocalError: local variable 'n_1' referenced before assignment EDIT: In my original post, I had not included n = 1 in the definition of fibGen