How to write the Fibonacci Sequence?

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

    def fib(lowerbound, upperbound):
        x = 0
        y = 1
        while x <= upperbound:
            if (x >= lowerbound):
                yield x
            x, y = y, x + y
    
    startNumber = 10
    endNumber = 100
    for fib_sequence in fib(startNumber, endNumber):
        print "And the next number is... %d!" % fib_sequence
    

提交回复
热议问题