Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Fibonacci sequence Darbandiman123 Programmer named Tim Posts: 17 Threads: 9 Joined: Dec 2016 Reputation: 0 Likes received: 0 #1 Sep-26-2018, 01:53 PM Hey i have this code which should print out fibonaccis sequence yet encounter the following error. ``````Error: Traceback (most recent call last): File "C:\Users\Louis\csyr9\fibonacci.py", line 12, in fibonacci() File "C:\Users\Louis\csyr9\fibonacci.py", line 5, in fibonacci while a <= 20: UnboundLocalError: local variable 'a' referenced before assignment ``````here is the code: ``` a = 1 b = 2 def fibonacci(): while a <= 20: c = a+b d = b+c print (a,b,c,d) a = c b = d fibonacci() ```how do i fix this? thanks ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #2 Sep-26-2018, 02:01 PM You need to define a and b in the function (indented under `def fibonacci`). Since you assign a value to a within the function, it assumes that variable is local to the function. Then it doesn't look outside the function for the value. But then on line 5 it can't find the value of a, since it hasn't been defined in the function. See the function tutorial link in my signature below for more information. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures Darbandiman123 Programmer named Tim Posts: 17 Threads: 9 Joined: Dec 2016 Reputation: 0 Likes received: 0 #3 Sep-26-2018, 02:32 PM Thanks! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post fibonacci ***Time limit exceeded*** frequency 18 1,636 Nov-29-2018, 09:03 PM Last Post: frequency Help debug my fibonacci implementation dineshpabbi10 6 876 May-16-2018, 12:12 PM Last Post: dineshpabbi10 Fibonacci Sequence on Turtle JRod 9 6,656 Feb-06-2017, 01:24 PM Last Post: JRod

Forum Jump:

Users browsing this thread: 1 Guest(s)