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: 4
和12000: 2
if length in dict:
dict[length] += amount
else:
dict[length] = amount
如果存在,则将新amount
添加到length
的字典条目中,如果尚不存在,则将其设置为amount
。