另外,如果要更方便,可以在前面使用 input 函数来限定函数的最大值:
maxx =int(input('斐波那契数列的最大值(忽略第一个数字0)'))
a , b =0,1
while(b <= maxx):
print(b)
a , b = b , a+b
也可以限制斐波那契数列的数量:
num =int(input('斐波那契数列的数量(忽略第一个数字0)'))
a, b =0,1
while(num >0):
print(b)
a , b = b , a + b
num = num -1
另外,利用无限循环函数可以让这个程序无限打印斐波那契数列的值:
a, b ,c=0,1,1
while(c ==1):
print(b)
a , b = b , a+b