修改您为上一个任务编写的函数,使其返回字典字典,而不是字典列表。字典应该是这样的
{
1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'}
}
这是我的代码。
def run():
dic_keys = ['Product', 'Brand', 'Cost']
products = {}
is_quit = False
while not is_quit:
product = {}
for key in dic_keys:
name = input("Enter {}: ".format(key))
if name == 'quit':
return products
product[key] = name
products.append(product) <<--- Do I need to append the product(dictionary) to the
products(dictionary outside while loop?
print(run())
以前,结果应如下所示:
[{'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
{'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'}]
现在应该是这样的:
{ 1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} }
字典的字典,而不是列表的字典。