Fibonacci sequence using list in PYTHON?

前端 未结 6 1429
清酒与你
清酒与你 2020-12-16 08:48

I have a problem about making a fibonacci sequence to a list, I\'m just new to python someone help me please.

This is my code. I know this is looking wrong or somet

6条回答
  •  悲哀的现实
    2020-12-16 09:18

    You may want this:

    In [77]: a = 0
        ...: b = 1
        ...: while b < 700:
        ...:     a, b = b, a+b
        ...:     print a, b
    1 1
    1 2
    2 3
    3 5
    5 8
    8 13
    13 21
    21 34
    34 55
    55 89
    89 144
    144 233
    233 377
    377 610
    610 987
    

    If you wanna store the results in a list, use list.append:

    In [81]: a = 0
        ...: b = 1
        ...: fibo=[a, b]
        ...: while b < 70:
        ...:     a, b = b, a+b
        ...:     fibo.append(b)
        ...: print fibo
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    

提交回复
热议问题