python 计算1/2+1/4+1/6+...+1/n,1/1+1/3+1/5+...+1/n。

编写一个函数,当输入n为偶数时,调用函数1/2+1/4+1/6+…+1/n。当输入n为奇数时,调用函数1/1+1/3+1/5+…+1/n。

def drab_sum(n):
    sum = 0.0
    if n % 2 == 0:
        for i in range(2,n+1,2):
            sum += 1 / i
    if n %2 != 0:
        for i in range(1,n+1,2):
            sum += 1 / i
    return sum
m = int(input("pls put into a number"))
n = drab_sum(m)
print(n)

"""
pls put into a number200
2.5936887588198103
"""

你可能感兴趣的:(Python)