1 # -*- conding:utf-8 -*- 2 # 定义一个n为偶数的函数 3 def even(n): 4 sum1 = 0.0 5 for i in range(2,n + 1,2): 6 sum1 += 1/i 7 return sum1 8 9 # 定义一个n为奇数的函数 10 def odd(n): 11 sum1 = 0.0 12 for i in range(1, n + 1, 2): 13 sum1 += 1 / i 14 return sum1 15 # 主程序 16 def main(): 17 n = int(input("请输入一个数")) 18 if n % 2 == 0: 19 sum = even(n) 20 print(sum) 21 else: 22 sum = odd(n) 23 print(sum) 24 main()