Modification of Future Value
<p>For this you have to add the annual contribution to the beginning of the year (the principal total) before computing interest for that year.</p>

<p>I am stuck and need help.  This is what I have so far:</p>

<pre><code>def main(): 

    print("Future Value Program - Version 2")
    print()
    principal = eval(input("Enter Initial Principal:"))
    contribution = eval(input("Enter Annual Contribution:"))
    apr = eval(input("Enter Annual Percentage Rate (decimal):"))
    yrs = eval(input("Enter Number of Years:"))
    for k in range (1, yrs):
        principal= principal * (1 + apr)
    print()
    print( yrs,) ": Amount $", int(principal * 100 + 0.5)/100)

main()
</code></pre>

<p>It is supposed to look like this:</p>

<pre><code>Future Value Program - Version 2 

Enter Initial Principal: 1000.00
Enter Annual Contribution: 100.00
Enter Annual Percentage Rate (decimal): 0.034
Enter Number of Years: 5 

Year 1: Amount $ 1137.4
Year 2: Amount $ 1279.47
Year 3: Amount $ 1426.37
Year 4: Amount $ 1578.27
Year 5: Amount $ 1735.33

The value in 5 years is $ 1735.33
</code></pre>

<p>Here's a working example that produces the expected output:</p>

<pre><code>def main(): 
    print("Future Value Program - Version 2")
    print()
    principal = float(input("Enter Initial Principal: "))
    contribution = float(input("Enter Annual Contribution: "))
    apr = float(input("Enter Annual Percentage Rate (decimal): "))
    yrs = int(input("Enter Number of Years: "))
    print()
    for yr in range(1, yrs + 1):
        principal += contribution
        principal = int(((principal * (1 + apr)) * 100) + 0.5) / 100
        print("Year {0}: Amount $ {1}".format(yr, principal))
    print()
    print("The value in {0} years is $ {1}".format(yrs, principal))

if __name__ == '__main__':
    main()
</code></pre>

<p>There were a few problems with the example in the question:</p>

<ol>
<li>A syntax error in the print statement on line 12. Calling <code>print</code> with parens means all the arguments should be enclosed inside the parens.  Python interpreted the errant paren as the end of arguments passed to <code>print</code>.</li>
<li>As noted by others, you shouldn't call <code>eval</code> on the inputs. Call <code>float</code> for floating point numbers, <code>int</code> for integers.</li>
<li>The <code>range</code> operator had an off by one error.</li>
<li>As noted by others, <code>print</code> is called outside of the loop, so intermediate states of the principal aren't output.</li>
<li>As far as basic maths, it seems as though adding the contribution was left out.</li>
<li>As per the expected output, the final <code>print</code> was missing.</li>
</ol>

python python-3.x