Norr - 1 year ago 55

Python Question

I'm learning the concepts of functions in Python and could use a little advice.

I'm trying to write a program that will use the algebraic function

`m(i) = (i) / (i + 1)`

Ideally this would be the result table I'm trying to get:

`i m(i)`

1 0.50

2 1.17

...

19 16.40

20 17.35

Currently what I have is:

`def equation(i):`

mi = ((i) / (i + 1))

return mi

def main():

for i in range(1,21):

print(format(i, '2d')," ",format(equation(i), '.2f'))

main()

The output I'm seeing in my shell is:

`1 0.50`

2 0.67

3 0.75

4 0.80

5 0.83

6 0.86

7 0.88

8 0.89

9 0.90

10 0.91

11 0.92

12 0.92

13 0.93

14 0.93

15 0.94

16 0.94

17 0.94

18 0.95

19 0.95

20 0.95

I feel like I'm off to a good start, but how can I add all the m(i) together for each new line? Like in the 'ideal' example I posted, it takes 0.5 + 0.67 giving 1.17 and so on.

Thank you in advance!

Answer Source

Try this:

```
def equation(i):
if i <= 0:
return 0
mi = ((i) / (i + 1))
return mi + equation(i - 1)
```