We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,201 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Creating a fibonacci sequence array from a to b

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]

4
Contributors
3
Replies
6 Days
Discussion Span
5 Months Ago
Last Updated
5
Views
Question
Answered
knd15
Deleted Member

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.

rubberman
Posting Maven
2,576 posts since Mar 2010
Reputation Points: 365
Solved Threads: 307
Skill Endorsements: 52

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

Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7
vegaseat
DaniWeb's Hypocrite
Moderator
6,476 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36
Question Answered as of 5 Months Ago by Ene Uran, rubberman and vegaseat

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1171 seconds using 2.68MB