【发布时间】:2019-11-23 18:32:44
【问题描述】:
account_balance = {'a': '122.8', 'b': '14.1', 'c': '31.44', 'd': '15.15', 'e': '23.07'}
total = 0.00
for key in account_balance:
total += float(account_balance[key])
然后这个:
print("TOTAL: {0}").format(str(total))
抛出一个错误:
AttributeError: 'NoneType' 对象没有属性 'format'
...为什么?
【问题讨论】:
-
您在
print()上调用.format(),而不是在字符串上。我建议使用 f-strings。 -
最终答案是:print("TOTAL: {0}".format(str(total))) 与 print("TOTAL: {0}").format(str(total))
标签: python error-handling