洗衣店的价目表是 {衣物: 价钱},你把一袋衣物 bag 交给店里。写 laundry_bill(prices, bag),把袋子里每件的价钱加起来;价目表上没有的(店里不洗)跳过,不算钱。
laundry_bill({"shirt": 5, "coat": 12}, ["shirt", "coat", "shirt"])→22laundry_bill({"shirt": 5}, ["sofa"])→0def laundry_bill(prices, bag):
total = 0
for item in bag:
try:
# 在这里写:查这件的价钱,加到 total 上
pass
except KeyError:
pass
return total
写好了先点「运行」试试示例,没问题再点「提交」。