Python练习题问题如下:
问题简述:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13
要求:求出这个数列的前20项之和。
Python解题思路分析:这道题要关注分子与分母的变化规律。2,3,5,8......
def f1():
a = 2.0
b = 1.0
s = 0
for n in range(1,21):
s += a / b
temp = a
a = a + b
b = temp
print (s)
def f2():
a = 2.0
b = 1.0
s = 0
for n in range(1,21):
s += a / b
#b,a=a,a+b,结果一样
a,b=a+b,a
print (s)
f1()
f2()
32.66026079864164
32.66026079864164