Elijah D-R
Elijah D-R

Reputation: 33

Error with Fibonacci sequence in python

Edit - All fixed thank you

fib=[0,1]
for i in range(0,700):
    fib.append(fib[len(fib)-2]+fib[len(fib)-1])
    print(fib[len(fib)-1])
print('Do you want a range of numbers or single?')
answer=input()
if answer=='single':
    print('Which number?')
    number=int(input())
    fib[number]
elif answer=='range':
    print('From:')
    firstNumber=int(input())
    print('To:')
    secondNumber=int(input())
    fib[firstNumber:secondNumber]

I have been trying to create a Fibonacci sequence in python which allows you to choose either which number to show or what range of numbers to show (script above). However when i run the script it runs fine at the start, i get to the part when you enter the number you want (either a single number, or the to and from numbers) but when i do nothing happens and the script ends. I am very new to python (coming from html and css, and i CBA right now to code this in HTML xD). Could anyone help me?

Upvotes: 0

Views: 307

Answers (1)

Octavio Soto
Octavio Soto

Reputation: 791

fib=[0,1]
for i in range(0,700):
    fib.append(fib[len(fib)-2]+fib[len(fib)-1])
    print(fib[len(fib)-1])
answer=input('Do you want a range of numbers or single?')
if answer=='single':
    number=int(input('Which number?[index]: '))
    print(fib[number])
elif answer=='range':
    firstNumber=int(input('From[index]: '))
    secondNumber=int(input('To[index]: '))
    print(fib[firstNumber:secondNumber])
else:
   print('Error')

Maybe this will work for you

Upvotes: 2

Related Questions