字典值“未定义”Python

问题描述 投票:0回答:2

我正在为课堂制作一份简单的杂货清单。第一步,我们创建一个空字典和列表,由用户的输入填充。所有杂货商品都会进入字典(grocery_item{}),并且所有字典都将添加到列表(grocery_history)中。

我的脚本目前如下所示:

grocery_item = {}

grocery_history =  []

stop = 'go'

while stop == 'go' or stop == 'c':
    item_name = input("Item name: \n" )
    quantitiy = int(input("Quantitiy purchased:\n" ))
    cost = float(input("Price per item:\n" ))
    grocery_item.update({'name' : item_name, 'number' : quantitiy, 'price' : 
                        cost})
    grocery_history.append(grocery_item)
    stop = input("Would you like to enter another item?\n Type 'c' for continue 
                 or 'q' to quite:\n")

如果我此时打印grocery_history,它将完全按照我的预期打印字典列表。在购物清单的下一步中,我们试图找到商品的总计。但是,每当我尝试使用 for 循环访问列表中的每个字典来查找每个项目的单独值时,我都会收到一条错误,声称键未定义,即使它只是打印了该杂货项目的字典条目,并且所有的键都有值。

我的本节脚本如下所示:

grand_total = 0

for item in grocery_history:
    I = 0
    for price in item:
        item_total = number * price
        grand_total += item_total
        print(number + name + '@' + '$' + price + 'ea' + '$' + round(item_total,2))
        item_total = 0
        I += 1

print('$' + round(grand_total,2))

错误发生在我尝试查找 item_total 的行,因为这是我尝试使用其中一个键的第一行。我尝试将键重写为grocery_item(numbers)/grocery_item(price),并收到相同的错误消息。

我感谢任何帮助,并提前致谢!

python python-3.x dictionary key-value
2个回答
0
投票

尝试这样的事情:

grocery_history = [] 
stop = 'go' 
while stop == 'go' or stop == 'c':
    grocery_item = {}
    grocery_item['name'] = input("Item name: \n" )  
    grocery_item['number'] = int(input("Quantitiy 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?\n Type 'c' for continue or 'q' to quit:\n")

grand_total = 0 
for item in grocery_history: 
    item_total = 0 
    item_total = item['number'] * item['price']  
    grand_total += item_total  
    print(item['number'], item['name'], '@', '$', item['price'], 'ea', '$', round(item_total,2))
print('$', round(grand_total,2))

如果要在字典上使用 for 循环,则需要循环字典的键。您可以通过使用字典的

.keys()
.items()
函数来做到这一点。但正如您在上面看到的,无需循环代码中的 dict 键。


0
投票
grocery_item = {}

grocery_history =  []

stop = 'go'

while stop == 'go' or stop == 'c':
    item_name = input("Item name: \n" )
    quantitiy = int(input("Quantitiy purchased:\n" ))
    cost = float(input("Price per item:\n" ))
    grocery_item.update({'name' : item_name, 'number' : quantitiy, 'price' :
                        cost})
    grocery_history.append(grocery_item)
    stop = input("Would you like to enter another item?\n Type 'c' for continue or 'q' to quit:\n")


grand_total = 0
for item in grocery_history:
    item_total = item['number'] * item['price']
    grand_total += float(item_total)
    print(str(item['number']) + " " +  str(item['name']) + ' @ ' + '$' + str(item['price']) + 'ea' + "\n" + '$'  + str(round(item_total,2)))
    item_total = 0
print("\n" + '$' + str(round(grand_total,2)) + " is the grand total.")
© www.soinside.com 2019 - 2024. All rights reserved.