Welcome to the CSC Q&A.
Get help and give help!
Write great code!
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.3k points)

3 Answers

+1 vote

have you already defined balance and totalinvest?

answered by (3.9k points)

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 (2.8k points)

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 (2.7k points)