Python程序-打印斐波拉契数列

2016-05-24 07:55:21来源:CSDN作者:tigerisland45人点击

这个程序的输出从第0项开始,输出到指定的项为止。

程序如下:

# 打印斐波拉契数列 iterations = int(input("Number of iterations: "))cont = 1result = ""if iterations > 0:    fibonacci1 = 0    fibonacci2 = 1    result = result + "" + format(fibonacci1)    result = result + ", " + format(fibonacci2)    while cont < iterations:        temp = fibonacci2        fibonacci2 = fibonacci1 + fibonacci2        fibonacci1 = temp        result = result + ", " + format(fibonacci2)        cont = cont + 1print("Fibonacci: " + result)
程序执行结果如下:

Number of iterations: 10
Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55


微信扫一扫

第七城市微信公众平台