1,105,581 Community Members

Creating a fibonacci sequence array from a to b

knd15
Deleted Member
 
0
 

I know this maybe easy but I need help with creating a fibonacci sequence array from a to b.
This is my code so far:

def FibL(a,b):
        list = []
        if a == 0:
            return 0
        elif a == 1:
            return 1
        else:
            return FibL(a-1) + (a-2)
        for i in range(a, b+1):
            list.append(i)
        return list

When I test it with print(FibL(1,6)), it returns 1.
I need it to return [1, 1, 2, 3, 5, 8]

Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

No mystery here. When it sees that a == 1, it returns immediately with a value of 1. It won't get to the list.append code.

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Oh my goodness, you are mixing a recursive fibonacci number function with an attempt to make a sliced list. A recipe for disaster.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 
Question Answered as of 1 Year Ago by Ene Uran, rubberman and vegaseat
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: