这个程序的输出从第0项开始,输出到指定的项为止。
程序如下:
# 打印斐波拉契数列 iterations = int(input("Number of iterations: ")) cont = 1 result = "" 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 + 1 print("Fibonacci: " + result)程序执行结果如下:
Number of iterations: 10
Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55