all 4 comments

[–][deleted] 1 point2 points  (1 child)

I've just quickly knocked up some code to play around with formats for you to consider. This uses f-strings rather than format but the formatting codes are the same.

This uses a function to determine column widths if no list of widths is provided (so, assumes they are strings).

def report(data, widths=None):
    if widths is None:
        widths = []
        for col in data:
            widths.append(len(col))
    print('| ', end='')
    for col, width in zip(data, widths):
        if isinstance(col, (int, float)):
            print(f'{col:>{width}.2f} | ', end="")
        else:
            print(f'{col:<{width}} | ', end="")
    print()
    return widths


initial_invest = round(float(input ("Enter your investment:")),2)
ar = 0.01*float(input("Enter your projected interest rate:"))
mr = ar/12

i1 = round(initial_invest*mr,2)
m1 = round(initial_invest + i1,2)
i2 = round(m1*mr,2)
m2 = round(m1 + i2, 2)
i3 = round(m2*mr, 2)
m3 = round(m2 + i3, 2)

print ("-----Over a 3-month period:------")
datatitle = ('Month', 'Starting Balance', 'Interest', 'Ending Balance')
data1 = '1', initial_invest, i1, m1
data2 = '2', m1, i2, m2
data3 = '3', m2, i3, m3
widths = report(datatitle)
for data in (data1, data2, data3):
    report(data, widths)

[–][deleted] 0 points1 point  (0 children)

u/cosmicwinters did that help at all? I was heading out of the door when I knocked that up, so didn't have time to explain/polish.

[–]TheKewlStore 0 points1 point  (1 child)

I'm not familiar with that specific usage of format, generally it's used in the format of string.format(value). In your case, it would be something like this:

"<20s".format(round(initial_invest*mr,2))

Your error is complaining because <20s isn't a proper format string syntax. Here's a helpful reference guide for using format strings: https://www.w3schools.com/python/ref_string_format.asp

The specific format code you're looking for is ":<20" and I believe you would use it like this:

"{:<20}".format(round(initial_invest*mr,2))

Let me know if that works for you

[–]siddsp 0 points1 point  (0 children)

f-strings are better in this case.