简体   繁体   中英

Write a recursive function to solve Fibonacci

I want to write a recursive function in Python for Fibonacci.

x will be the starting point, y will be the subsequent of x and l is the length.

I don't understand what is my error in thinking:

 def fib(x, y, l, fibList=None):
    fibList = []
    z = x + y
    x = y
    fibList.append(z)
    y = z

    if len(fibList) <= l:
        return fib(x, y, l, fibList)
    else:
        return(fibList)

The result is:

RecursionError: maximum recursion depth exceeded while calling a Python object

I can solve it with a for loop but not with recursive function.

There are a few problems here. Once you fix the infinite recursion, you still have an issue.

As @Raqha points out, you need to not initialize your list every time the fib function is called, but rather only the first time, when the fibList parameter isn't supplied and so defaults to None .

Your code is not able to generate the first two numbers in the sequence, 0 and 1 . You can fix this by simply initializing your list to include these terms, and adjust the logic to only provide N-2 more terms.

The signature of your function could be improved to make it much easier for the caller to use it. The caller only cares about the number of terms he/she wants. The user shouldn't have to know what to enter for the initial x and y values.

Here's a version of your code with the fix for the infinite recursion, the fix for the missing terms, and also with the signature rearranged so that the user can call the function simply and obviously:

def fib(l, x=0, y=1, fibList=None):
    if fibList is None:
        fibList = [0, 1]
    z = x + y
    x = y
    fibList.append(z)
    y = z

    if len(fibList) < l-1:
        return fib(l, x, y, fibList)
    else:
        return(fibList)

print(fib(10))

Result:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

At the start of every fib function call you clear your fibList with fibList = [] . So the list's length will always be <= 1 , thus you run into an infinite recursion loop which isn't good. You need to add something like if fibList is None:

When you first call the function "fib" without providing any 4th statement in the argument list, the value of fibList will initially set to "None". But later when you recursively call the function "fib" again, you provide a fibList in the argument list. So the value isnt "None" anymore. So when adding an if-statement as mentioned above, the function knows when you call the function from the outside (when you call it in your code as "fib(1,2,10)"), or when the function recursively calls itself. And so you dont reset the fibList everytime you call the function, but set it only 1 time at the start.

def fib(x, y, l, fibList=None):
    if fibList is None:
        fibList = []
    z = x + y
    ...

On the second line you set fibList = [] . This means that every time you call the function recursively it resets the list to be empty so len(fibList) will always equal 1.

Remove that line so the fibList variable is not reset, then it should hit your exit condition correctly. How it is written now it runs forever until it breaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM