while循环
def feb(limit):
'''
feb函数用于打印斐波那契数列
参数说明:
limit:斐波那契数最大取值范围值,需输入正整数
例:feb(100)打印100以内的斐波那契数列
'''
l = [0,1]
while True:
x = len(l)
if l[x-1] <= limit:
l.append(l[x-1]+l[x-2])
else:
break
print(l[:-1])
feb(100)
feb(1000)
# 输出结果:
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
for循环
def feb(count,limit):
l = [0,1]
for i in range(count-1):
x = len(l)
if l[x-1] <= limit:
l.append(l[x-1]+l[x-2])
else:
break
print(l[:-1])
feb(5,10)
feb(15,200)
# 输出结果:
# [0, 1, 1, 2, 3]
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]