Python fibbonaci sequence

Question:

I’m a beginner with python, and I’m trying to make a script that will print the fibonacci sequence as a list, or a specified number in the sequence based on a given number. This probably sounds confusing, so let me show you guys the code, and then explain.

number = 1
last = 0
before_last = 0

def Fibonacci(number, last, before_last):
    Question = raw_input('Would you like a List or the Number: ')

    X = raw_input('Give a number: ')
    print number

    if Question == "List":
        for counter in range(0, int(X) - 1):
            before_last = last
            last = number
            number = before_last + last
            print number

    if Question == "Number":
        for counter in range (int(X) - 2, int(X) - 1):
            before_last = last
            last = number
            number = before_last + last
            print number

Fibonacci(number, last, before_last)

Basically, you choose list or number, and you give the code your number. The number is taken, and used in the range. However, with the singular number, it will only print the number one. Why is this, and how do i fix it, so that if I gave the code the number 10 as input, it would print the 10th number in the Fibonacci sequence? An explanation and how to fix it would be quite helpful, and if any of you can give helpful advice, that would be amazing.

Asked By: supersmarty1234

||

Answers:

You can try using a recursive function like this:

def fib(x):
    if x<2:
            return x
    return fib(x-2) + fib(x-1)

Then, you can change your if statements to these.

if Question == "List":
    for counter in range(0, int(X)):
        print fib(counter)
if Question == "Number":
    print fib(X)

Then you can calculate each value of the fibonacci series using this based on if you need a number or a list.

Answered By: meyer9

You code only prints one number because the range is always equal to 1. e.g. range (int(X) – 2, int(X) – 1) will always be equal to 1.

Answered By: Joel Gregory

There’s an answer on how you could do the fib sequence, but your actual error was in this line:

for counter in range (int(X) - 2, int(X) - 1):

What it’s actually supposed to be is from

range(0, int(X) - 1):

because you’ll want to calculate up to that number, and then display it. Currently you’re only calculating the first number of the sequence and displaying that. You might also want to move the print statement out of the for loop.

Answered By: TravelingMaker

You can use a List to keep track of all numbers in the sequence, and then print whichever number you want. Try this:

number = 1
last = 0
before_last = 0

def Fibonacci(number, last, before_last):
    Question = raw_input('Would you like a List or the Number: ')
    l=[1]
    X = raw_input('Give a number: ')

    if Question == "List":
        print number
        for counter in range(0, int(X) - 1):
            before_last = last
            last = number
            number = before_last + last
            print number

    if Question == "Number":
        for counter in range (0, int(X)):
            before_last = last
            last = number
            number = before_last + last
            l.append(number)
        print l[int(X)-1]

Fibonacci(number, last, before_last)

Output:

#Would you like a List or the Number: List
#Give a number: 6
#1
#1
#2
#3
#5
#8

#Would you like a List or the Number: Number
#Give a number: 6
#8
Answered By: Ujjwal

When Question == "Number", your for loop iterates over the sequence range (int(X) - 2, int(X) - 1). When you input 10 for X, this evaluates to range(8, 9), which is of length one. As a result, the loop is only executed once. Since both loops are otherwise the same, and count is never used inside the loop body, you get the same result as if you ran the other loop with X = 1.

In both cases, you want to loop over range(0, x-1), accumulating the value of the next number in the sequence at each iteration. The only difference is whether you print the final result or print the intermediate values as well.

Keeping the existing structure of your code intact, this is as simple as:

if Question == "Number":
    for counter in range (0, int(X) - 1): # Loop over the same range
        before_last = last
        last = number
        number = before_last + last
    print number # But print the number outside the loop

You may want to note that your code probably isn’t quite the way a seasoned programmer would write it. That’s ok. You’re just beginning, and have a lot to learn. I don’t want to inundate you with details. However, as a start, I think you should think about moving the number, last, and before_last variables into the Fibonacci function (i.e., make them local to the function, removing both the function parameters and the global variables). There are other improvements you could make (some of which may be mentioned in other answers), but start there.

Answered By: Nathan Davis

This solution might shed some light on your problem and help solve it.

Question = raw_input("Enter Type: ")
num = raw_input("Terms: ")
def fibonacci(n):
    if n <= 1:
        return n
    else:
        print(fibonacci(n-1) + fibonacci(n-2))



# take input from the user
if Question == "List":
    for counter in range(0, int(num)):
        print fibonacci(counter)
#find specific number in list and print it out
if Question == "Number":
    if(num < 2):
        return num:
    else:
        for n in range(num):
            print(fibonacci(n)[num]) 

Sorry I haven’t used Python for 2 years, I just found this question interesting

Source -> Link

Answered By: Irrational Person
import time as t
t0 = 1
t1 = 1
while t1 < 10**100:
    t0 = t1 + t0
    t1 = t1 + t0
    print('{:,}'.format(t0).replace(',',' '))
    t.sleep(.1)
    print('{:,}'.format(t1).replace(',',' '))
    t.sleep(.1)

Uses recursion and produces the same output

Answered By: user20223218
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.