简体   繁体   中英

I'm getting this error : Traceback (most recent call last): File "./prog.py", line 4, in <module> KeyError: 'price'

prices = ['31', '32']
txt = "For only {price:.2f} dollars!"
for i in prices:
    print(txt.format(prices))

Im trying this to get: For only 31.00 dollars For only 32.00 dollars but am facing this issue. Please help, Im a noob

prices = ['31', '32']
txt = "For only {price:.2f} dollars!"
for i in prices:
    print(txt.format(prices))

You were about 80% there.
Try the following code:

Code:

prices = ['31', '32']

for i in prices:
    print(f"For only {int(i):.2f} dollars!")

Output:

For only 31.00 dollars!
For only 32.00 dollars!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM