宠物医院的记录一行一只:名字和体重(公斤,整数),中间一个空格。写 read_weights(text),变成 {名字: 体重} 的字典,体重要是数字。同一只称了两次,留后面那次。
read_weights("Momo 4\nRex 12")→{'Momo': 4, 'Rex': 12}read_weights("")→{}def read_weights(text):
weights = {}
for line in text.splitlines():
name, kg = line.split()
# 在这里写:把体重变成数字,存进字典
return weights
写好了先点「运行」试试示例,没问题再点「提交」。