0

I'm trying to write a program to print the first 100 Fibonacci numbers. This is my code:

def main():

    print("The first 100 Fibonacci numbers are: ")
    fibonacciList = (0,0,1)
    loop = False

    while not loop:

        listLength = len(fibonacciList)
        newFibonacci = fibonacciList[-1] + fibonacciList[-2]
        fibonacciList = newFibonacci + fibonacciList


        if (listLength > 103):
            loop = true

        print(fibonacciList)

main()

When I run it I get the error below referring to the fibonacciList = newFibonacci + fibonacciList line:

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

I don't understand what's wrong.

P.S.: The two zeroes in the Fibonacci list are there to prevent a few other errors I got earlier.

3
  • 1
    In line fibonacciList = newFibonacci + fibonacciList, you are performing + over newFibonacci which holds int value and fibonacciList which is a tuple Commented Jan 15, 2017 at 14:37
  • 1
    Adding two zeros to the list isn't the right way to fix whatever problem you have. Only one sequence defined by F(n) = F(n-1) + F(n-2) starts with 2 0s: the infinite list of 0s. Commented Jan 15, 2017 at 14:38
  • 1
    fibonacciList = (*fibonacciList, newFibonacci) might be closer to what you are looking for Commented Jan 15, 2017 at 14:40

3 Answers 3

1

Try this:

print("The first 100 Fibonacci numbers are: ")
fibonacciList = (0,0,1)
loop = False

while not loop:

    listLength = len(fibonacciList)
    newFibonacci = fibonacciList[-1] + fibonacciList[-2]
    fibonacciList =  fibonacciList + (newFibonacci,)


    if (listLength > 103):
        loop = True

print(fibonacciList)

You can't add an int to a tuple, but you can concatenate two tuples with +, so we can tuple-ize the int with (newFibonacci,).

There are more efficient and readable ways to compute this sequence, but this solves your immediate question.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

def main():

    print("The first 100 Fibonacci numbers are: ")
    fibonacciList = [0,0,1]
    loop = False

    while not loop:

        listLength = len(fibonacciList)
        newFibonacci = fibonacciList[-1] + fibonacciList[-2]
        fibonacciList.append(newFibonacci)


        if (listLength > 103):
            loop = True
            print(fibonacciList)

main()

Comments

0

In addition to the other answers you could try the following solution, which is faster, more efficient and better readable:

def fib(n):
    fiblist = []
    x, y = 0, 1

    for i in range(n):
        fiblist.append(x)
        x, y = y, x + y

    print('The first {} Fibonacci numbers are: \n{}'.format(n, fiblist))

Then for the first 100 Fibonacci numbers call the function as follows:

fib(100)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.