Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fibonacci sequence
#1
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 <module> 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
Reply


Messages In This Thread
Fibonacci sequence - by Darbandiman123 - Sep-26-2018, 01:53 PM
RE: Fibonacci sequence - by ichabod801 - Sep-26-2018, 02:01 PM
RE: Fibonacci sequence - by Darbandiman123 - Sep-26-2018, 02:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fibonacci Yasunaga 7 4,638 May-16-2021, 02:36 PM
Last Post: csr
  fibonacci ***Time limit exceeded*** frequency 18 13,712 Nov-29-2018, 09:03 PM
Last Post: frequency
  Help debug my fibonacci implementation dineshpabbi10 6 5,448 May-16-2018, 12:12 PM
Last Post: dineshpabbi10
  Fibonacci Sequence on Turtle JRod 9 14,955 Feb-06-2017, 01:24 PM
Last Post: JRod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020