How to write the Fibonacci Sequence?

前端 未结 30 2321
醉酒成梦
醉酒成梦 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:15

    this is an improvement to mathew henry's answer:

    def fib(n):
        a = 0
        b = 1
        for i in range(1,n+1):
                c = a + b
                print b
                a = b
                b = c
    

    the code should print b instead of printing c

    output: 1,1,2,3,5 ....

提交回复
热议问题