繁体   English   中英

我收到此错误:Traceback(最近调用最后一次):文件“./prog.py”,第 4 行,在<module>关键错误:“价格”</module>

[英]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))

我正在尝试这样做: 只需 31.00 美元 只需 32.00 美元,但我正面临这个问题。 请帮忙,我是菜鸟

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

你在那里大约有 80%。
试试下面的代码:

代码:

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!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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