Python Fibonacci Generator

前端 未结 17 1416
别那么骄傲
别那么骄傲 2020-11-27 20:59

I need to make a program that asks for the amount of Fibonacci numbers printed and then prints them like 0, 1, 1, 2... but I can\'t get it to work. My code looks the followi

17条回答
  •  被撕碎了的回忆
    2020-11-27 21:31

    I've build this a while ago:

    a = int(raw_input('Give amount: '))
    
    fab = [0, 1, 1]
    def fab_gen():
        while True:
            fab.append(fab[-1] + fab[-2])
            yield fab[-4]
    
    fg = fab_gen()
    for i in range(a): print(fg.next())
    

    No that fab will grow over time, so it isn't a perfect solution.

提交回复
热议问题