在这件事上不知所措。我得到了一个键错误,但我不明白为什么引用的键看起来像是在dict中
有什么帮助吗?在
TEMPLATE = "{ticker:6s}:{shares:3d} x {price:8.2f} = {value:8.2f}"
report = []
stock = {'ticker': 'AAPL', 'price': 128.75, 'value': 2575.0, 'shares': 20}
report.append(TEMPLATE.format(stock))
这是我得到的错误:
^{pr2}$
您需要将
**
放在dictionary参数前面。最后一行是:它应该会起作用的。在
所以你的代码应该是:
^{pr2}$相关:Python 3.2: How to pass a dictionary into str.format()
相关问题 更多 >
编程相关推荐