Python 3.5.1 Formatting Floats Issue
<p>I am trying to create a app that compares loans with various interest rates for a programming assignment. I generally understand what I am doing and could complete the assignment but am running into an issue with the .format function. I'm trying to format floats so that they can be printed as results. Here is my code: </p>

<pre><code># Prompts the user to enter a loan amount
loan_amount = eval(input("Enter loan amount in dollars (exclude commas):"))

# Prompts the user to enter a time period
length_years = eval(input("Enter the amount of years as an integer:"))

# Displays the header
print("{0:20s}{1:20s}{2:20s}".format("Interest Rate", "Monthly Payment",
                                      "Total Payment"))

interest_rate = 5.0
while (interest_rate &lt;= 8.0):
    monthly_interest_rate = interest_rate / 12
    monthly_payment = (monthly_interest_rate / 100) * loan_amount
    total_payment = ((interest_rate / 100) * length_years) * loan_amount
    print("{&lt;20.6f}{&lt;20.6f}{&lt;20.6f}".format(interest_rate, monthly_payment, total_payment))
    interest_rate = interest_rate +.25
</code></pre>

<p>And this is the error I receive: </p>

<pre><code>Enter loan amount in dollars (exclude commas):1000
Enter the amount of years as an integer:10
Traceback (most recent call last):
Interest Rate       Monthly Payment     Total Payment       
  File "/Users/Andrew/PycharmProjects/PA1/pa1_main.py", line 45, in &lt;module&gt;
    main()
  File "/Users/Andrew/PycharmProjects/PA1/pa1_main.py", line 42, in main
    print("{&lt;20.6f}{&lt;20.6f}{&lt;20.6f}".format(interest_rate, monthly_payment, total_payment))
KeyError: '&lt;20'

Process finished with exit code 1
</code></pre>

<p>You can omit the numbering (and it'll be auto-numbered for you), but you still need to use the <code>:</code> colon to separate the identifier from the formatting specification:</p>

<pre><code>print("{:&lt;20.6f}{:&lt;20.6f}{:&lt;20.6f}".format(interest_rate, monthly_payment, total_payment))
</code></pre>

<p>Without the <code>:</code> separator, Python interprets the content before a <code>.</code> as a keyword argument to look up (and <code>.6f</code> as an attribute on the object).</p>

<p>As a side note: rather than use <code>eval()</code> (and open your script to abuse), I'd use <code>int()</code> (for whole numbers), and <code>float()</code> or <code>decimal.Decimal()</code> to convert input into the correct type:</p>

<pre><code>loan_amount = float(input("Enter loan amount in dollars (exclude commas):"))
length_years = int(input("Enter the amount of years as an integer:"))
</code></pre>

python floating-point python-3.5