有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前n项之和,n由用户输入。(用循环语句)
时间: 2023-12-13 07:04:57 浏览: 22
numerator1 = 2
denominator1 = 1
numerator2 = 3
denominator2 = 2
sum = 2
n = int(input("请输入要求的项数:"))
for i in range(3, n+1):
numerator3 = numerator1 + numerator2
denominator3 = denominator1 + denominator2
sum += numerator3/denominator3
numerator1 = numerator2
denominator1 = denominator2
numerator2 = numerator3
denominator2 = denominator3
print("前", n, "项之和为:", sum)
阅读全文