Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+5 votes

I having trouble with last two print statements. I am getting an error message saying that it is invalid format specifier.

my print statements are:

print(f'Balance after, {numYears:,}, years: $, {balance:,.2f}')
print(f'Total amount invested: $, {totalInvest:,.2f}')
asked in CSC201 Spring 2021 by (1 point)

3 Answers

+1 vote

have you already defined balance and totalinvest?

answered by (1 point)

Yes I have defined the both of the variables

+1 vote

When using the f-string, you don't need a comma outside of the variable. If you do that, the commas will also appear in the output.

However, with comma or not, the code runs just fine. Can you send me the error message when you run in?

answered by (1 point)

The error says

Traceback (most recent call last):
     File "/Users/chloefausett/Documents/CSC 201/Routine 9.py" , line 37, in <module>
print(f'Balance after {numYears:,} years: $ {balance:,.2f}')
ValueError: Invalid format specifier

Sorry, it won't let me add an image!

+1 vote

I used the .format( ) way instead and that worked for me if you haven't tried that already!

answered by (1 point)