修改字典中的值,添加金额



Code

dict = {}
flag = True
while True:  
length = raw_input('enter length')
amount = raw_input('enter amount')
if (length == 'quit') or (amount == 'quit'):
flag = False
continue

我想添加代码,所以

if key in dict:
# previous value + amount
# insert the new value to the key
else: 
dict[length] = amount

例如: 我的输入是:13000,2比:12000: 2再说一遍:13000: 2

所以字典将包括13000: 412000: 2

if length in dict:
dict[length] += amount
else:
dict[length] = amount

如果存在,则将新amount添加到length的字典条目中,如果尚不存在,则将其设置为amount

最新更新