#!/usr/bin/env python# coding:UTF-8"""@version: python3.x@author:曹新健@contact: 617349013@qq.com@software: PyCharm@file: 1223.py@time: 2018/12/23 20:56"""'''有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和。'''"""#方法一def g(n):if n <= 2:return nelse:return g(n-1) + g(n-2)sum = 0for i in range(1,21):sum += g(i+1)/g(i)print(sum)"""#方法二numerator=2denominator=1sum=0whileTrue:try:n=int(input("請輸入一個整數:"))exceptValueError:("輸入錯誤,請輸入整數")else:foriinrange(n):sum+=numerator/denominatornumerator, denominator=numerator+denominator, numerator(sum)break
