-1

我正在尝试创建一个包含字典列表的简单程序。杂货历史是字典列表,字典是杂货项目。这是代码:

'''
The task is broken down into three sections.
Section 1 - User Input
Section 2 - loop through the grocery list
Section 3 - provide output to the console
'''


grocery_item = {}
grocery_history = []
stop = 'go'

while stop != 'q':
    item_name = input("Item name:\n")
    quantity = int(input("Quantity purchased:\n"))
    cost = float(input("Price per item:\n"))

    grocery_item['name'] = item_name
    grocery_item['number'] = quantity
    grocery_item['price'] = cost

    grocery_history.append(grocery_item)

    stop = input("Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n")
    print(grocery_history)

grand_total = 0 

for grocery_item in range(0, len(grocery_history)):

  item_total = grocery_history[grocery_item]['number'] * grocery_history[grocery_item]['price']
  grand_total += item_total

  print(str(grocery_history[grocery_item]['number']) + " " + grocery_history[grocery_item]['name'] + " @ $%.2f" % grocery_history[grocery_item]['price'] + " ea \t$%.2f" % item_total)

  item_total = 0.0

print("Grand total: $%.2f" % grand_total)
print(grocery_history)

如果您想知道,这个赋值的提示告诉我在我的 for 循环中使用变量grocery_item。我通常会选择一个不同的名字,因为它变得令人困惑。我还添加了一些打印语句来打印出grocery_history 的内容以查看出了什么问题,并且我确认它是在将字典添加到grocery_history 列表时,因为某种原因它会更新现有的字典项以匹配新的一个正在添加。我无法弄清楚我做错了什么。任何帮助将不胜感激!

4

4 回答 4

3

您每次都需要在循环中定义一个新的字典对象,否则您最终会使用相同的字典对象,因此实际上会替换已经添加的字典对象。

while stop != 'q':
    grocery_item = {}
    # ...
于 2019-11-21T18:19:58.463 回答
1

这是因为您的循环不会grocery_item每次都创建一个新对象;它只是一遍又一遍地更新同一个对象。 grocery_item

grocery_history最终包含对该单个对象的多个引用,处于其最新状态。

要解决此问题,请将线移动到循环grocery_item = {}下方的第一项 。这将在每次循环中while stop != 'q':创建一个新对象。

于 2019-11-21T18:21:36.947 回答
1

示例代码应该可以工作


grocery_history = []
stop = 'go'

while stop != 'q':
    item_name = input("Item name:\n")
    quantity = int(input("Quantity purchased:\n"))
    cost = float(input("Price per item:\n"))

    grocery_item = {}
    grocery_item['name'] = item_name
    grocery_item['number'] = quantity
    grocery_item['price'] = cost

    grocery_history.append(grocery_item)

    stop = input("Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n")
    print(grocery_history)

grand_total = 0

for grocery_item in range(0, len(grocery_history)):

  item_total = grocery_history[grocery_item]['number'] * grocery_history[grocery_item]['price']
  grand_total += item_total

  print(str(grocery_history[grocery_item]['number']) + " " + grocery_history[grocery_item]['name'] + " @ $%.2f" % grocery_history[grocery_item]['price'] + " ea \t$%.2f" % item_total)

  item_total = 0.0

print("Grand total: $%.2f" % grand_total)
print(grocery_history)

问题的发生是因为您重复使用相同的 dict,虽然它每次都添加到列表中,但您实际做的是不断更新相同的引用,因此列表中的所有内容都变成相同的值移动 dict 里面解决问题

于 2019-11-21T18:20:26.643 回答
0

您可以使用mapandsum来获取 grand_total

grocery_history = []
stop = 'go'

while stop != 'q':
    grocery_item = {}

    grocery_item['name'] = input("Item name:\n")
    grocery_item['number'] = int(input("Quantity purchased:\n"))
    grocery_item['price'] = float(input("Price per item:\n"))

    grocery_history.append(grocery_item)

    stop = input("Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n")

grand_total = sum(map(lambda item: item['number'] * item['price'], grocery_history))

print("Grand total: $%.2f" % grand_total)
print(grocery_history)

输入:

Item name:
item
Quantity purchased:
2
Price per item:
1.2
Would you like to enter another item?
Type 'c' for continue or 'q' to quit:
q

输出:

Grand total: $2.40
[{'name': 'item', 'number': 2, 'price': 1.2}]
于 2019-11-21T18:22:38.773 回答