Sum Series with Functions - Continously adding previous answers?
<p>I'm learning the concepts of functions in Python and could use a little advice. </p>

<p>I'm trying to write a program that will use the algebraic function <code>m(i) = (i) / (i + 1)</code> for inputs 1 through 20. So far I've got that to work, but now I want to add all previous outputs together for each new output. It could be I'm just not grasping the mathematical concept of this and that's why I'm not getting it right in my code.</p>

<p>Ideally this would be the result table I'm trying to get:</p>

<pre><code>i      m(i)     

1      0.50
2      1.17
...
19    16.40
20    17.35
</code></pre>

<p>Currently what I have is:</p>

<pre><code>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()
</code></pre>

<p>The output I'm seeing in my shell is:</p>

<pre><code> 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
</code></pre>

<p>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.</p>

<p>Thank you in advance!</p>

<p>You can do something like this. </p>

<pre><code>#!/usr/bin/python

def equation(i):
    mi = (float(i) / (i + 1))
    return mi

def main():
    sumOfMi = 0
    for i in range(1,21):
        sumOfMi+= equation(i)
        print(format(i, '2d'),"    ",format(sumOfMi, '.2f'))

main()
</code></pre>

python function math sum continuous