+ 1
Please help syntax error
Here is my line Print('The sale price is
#x27;, format(sale_price, ',.2f'), sep='') When i try to run the code its givint me an invalid syntax errors. Can someone please tell me what i am doing wrong? Thank you3 Antworten
+ 8
I think what you are trying to do is this:
print("The sale price is {:.2f}quot;.format(sale_price))
edited:
- format is a method of string. this means you should use a dot (.) and not a comma (,) between it and the string.
- the place inside the string you want the sale_price value to appear you should put braces {}.
- the precision goes inside the braces.
+ 5
'text {}'.format( stuff_to_insert_at_braces )
print( '{}'.format(price) ) # raw insert
print( '{:6.2f}'.format(price) )
# 6 spots total. Three on the left, the decimal and two to the right (3+1+2=6). 'f' means 'float'.
You have 'sep' okay. Pay attention to periods, capitalization ('Print' is not 'print') and comma placement.
+ 3
...Noticed you're trying to use the format() function (not the method)...which explains the separator. The format *function* (from docs: PEP 3101) calls the object's __format__ method.
This works for the function:
print("string ", format(1.244, ".2f"), sep="")
because the float (object) already has the formatting context. Attempts to use {} or :, % etc. cause syntax errors...because those are for strings, which don't know they're getting floats (etc) inserted.
This was tricky to find, and if you don't have to do this, you might have clearer code by using the other way (which seems to be preferred).