def fenshu(n):
"""调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n"""
sum = 0
# 判断n的奇偶性
if n % 2 == 0:
# 求其前n/2项偶数的倒数和
for i in range(2, n + 1, 2):
sum += 1 / i
return sum
else:
# 求其前n/2项奇数的倒数和
for i in range(1, n + 1, 2):
sum += 1 / i
return sum
print("前n/2项的偶数(奇数)的倒数和为:" + str(fenshu(8)))