下面是自己写着玩的:
a=1
b=1
temp=int(input(“输入第几个数:”))
if temp == 1:
print(“第”,temp,“个数是”,0)
elif temp2 or temp3:
print(“第”,temp,“个数是”,1)
else:
for i in range(temp-3):
sum=a+b
a=b
b=sum
print(“输出第”,temp,“个数是:”,b)
================================================================================菜鸟网站上的例子
-- coding: UTF-8 -- Filename : test.py author by : www.runoob.com Python 斐波那契数列实现 获取用户输入数据nterms = int(input(“你需要几项?”))
第一和第二项n1 = 0
n2 = 1
count = 2
if nterms <= 0:
print(“请输入一个正整数。”)
elif nterms == 1:
print(“斐波那契数列:”)
print(n1)
else:
print(“斐波那契数列:”)
print(n1,",",n2,end=" , “)
while count < nterms:
nth = n1 + n2
print(nth,end=” , ")
# 更新值
n1 = n2
n2 = nth
count += 1
执行以上代码输出结果为:
你需要几项? 10斐波那契数列:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,
下面是网友的总结:
https://www.cnblogs.com/areyouready/p/8979973.html
https://www.cnblogs.com/panlq/p/9307203.html
https://www.runoob.com/python3/python3-fibonacci-sequence.html