因为函数必须返回的是字典字典,而不是字典列表,
我很难把字典放在一本空字典里。因为它是嵌套的字典,
追加不起作用,因为它不是字典列表。
你们能找出错误在哪里吗?
这是我的代码。
def run():
name_dict = ['Product', 'Brand', 'Cost']
product = {}
while True:
item = {}
for key in name_dict:
input_item = input("Enter {}: ".format(key))
if input_item == 'quit':
return product
item[key] = input_item
print()
product = run()
print(product)这就是问题所在。
发布于 2020-06-21 20:54:21
您忘记使用item更新product。将此行放在item[key] = input_item之后
product[str(item['Product'])] = item这会将项目添加到产品字典中。作为关键字,我使用用户输入作为Product name。如果用户只使用数字来命名产品,则使用str()。
https://stackoverflow.com/questions/62498520
复制相似问题