我正在尝试为字典中的键分配一个新值。但是得到“ValueError:int()的无效文字,以10为基数:”这就是我所做的。
balance = {'beer':5, 'toast':2}
item = input("what did you eat?: ")
price = input("how much?: ")
if item not in balance:
balance[item] = int(price)
else:
balance[item] = balance[item] + int(price)
我很困惑,因为我可以在 python shell 中做 balance['beer'] = 5,我错过了什么?