How to write the Fibonacci Sequence?

前端 未结 30 2324
醉酒成梦
醉酒成梦 2020-11-22 00:32

I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 01:25

    use recursion:

    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n-1) + fib(n-2)
    x=input('which fibonnaci do you want?')
    print fib(x)
    

提交回复
热议问题