0

I'm in an intro programming class and am lost. We've had several labs that required knowledge that we haven't been taught but I've managed to find out what I need on google (as nobody responds to the class message board) but this one has me pretty frustrated. I'll include a pastebin link here: https://pastebin.com/6JBD6NNA

`principal = input()
print('Enter the Principal Value of your investment: $', float(principal))

time = input()
print('\nEnter the Time(in years) you plan to save your investment: ', int(time))

rate = input()
print('\nEnter the Rate (2% = 0.02) you will collect on your investment: ', float(rate))


interest = (float(principal) * float(rate)) * int(time)
final_value = float(principal) + float(interest)
print('\nThe Final Value of your investment will be: $%.2f' % final_value)`

So I need the output of the dollar amounts to have a comma ($27,500.00) but I have no idea how to do this. I've seen a couple of solutions on this site and others but I can't get them to work. PLEASE can someone help me?

  • have you tried `"{:,}".format(value)` from https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators – deltatango Sep 25 '17 at 18:08
  • Yes I couldn't get that to work; I'm only in my 4th week of the class so you all will have to bear with me. :P After examining the solutions below I see what I was doing wrong. Thanks everyone! – Brian Grubba Sep 25 '17 at 18:31

3 Answers3

2

In Python 2.7 or above, you can use

print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))

This is documented in PEP 378.

Source: Python Add Comma Into Number String

Stuart Buckingham
  • 1,574
  • 16
  • 25
  • When I try that with my variable I get "SyntaxError: EOL while scanning string literal" – Brian Grubba Sep 25 '17 at 18:10
  • In your original question, you have the entire code block surrounded in back ticks (why?). Maybe you need to add them again. This works fine for me: `>>> print('The Final Value of your investment will be: ${:,.2f}'.format(23456789)) The Final Value of your investment will be: $23,456,789.00` – Stuart Buckingham Sep 25 '17 at 18:11
  • Not sure why those are there, they aren't in my original code. Perhaps when I turned it into a code block they were added. – Brian Grubba Sep 25 '17 at 18:29
1

Your last line should be:

print ("\nThe Final Value of your investment will be: ${:,.2f}".format(final_value))
Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86
  • so is the only difference the double quotation? Thank you that did work (I meant the only difference between your solution and the one from Stuart above) – Brian Grubba Sep 25 '17 at 18:12
1

Here is a working example:

principal = float(input('Enter the Principal Value of your investment: $'))

time = int(input('\nEnter the Time(in years) you plan to save your investment: '))

rate = float(input('\nEnter the Rate (2% = 0.02) you will collect on your investment: '))


interest = principal * rate * time
final_value = principal + interest
print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))
Kyle Higginson
  • 922
  • 6
  • 11