Python Exercise Example76
# Python2.x Python Exercise 76
[ Python 100 Examples](#)
**Question:** Write a function. When the input n is an even number, call the function to calculate 1/2+1/4+...+1/n. When the input n is an odd number, call the function to calculate 1/1+1/3+...+1/n.
**Program Analysis:** None.
Program source code:
## Example(Python 2.0+)
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def peven(n):
i = 0
s = 0.0
for i in range(2, n + 1, 2):
s += 1.0 / i # In Python, integer division by integer only yields an integer, so you need to use the float 1.0
return s
def podd(n):
s = 0.0
for i in range(1, n + 1, 2):
s +=
YouTip