'Dictionary value "not defined" Python
I'm working on a simple grocery list for class. In the first step we create an empty dictionary and list to be filled by input from a user. All grocery items go into a dictionary (grocery_item{}) and all of the dictionaries are to be added to the list (grocery_history).
My script currently looks like this:
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")
If I print grocery_history at this point, it will print the list of dictionaries exactly as I intended. In the next step of the grocery list, we are trying to find the grand total of the items. However, whenever I try to find each items individual value, using for loops to get to each dictionary in the list, I receive an error claiming that keys are not defined, even though it just printed the dictionary entry for that grocery item, and all of the keys had values.
My script for this section looks like this:
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))
The error is for the line in which I am trying to find the item_total, because it is the first line I try to use one of the keys. I have tried to rewrite the keys as grocery_item(numbers)/grocery_item(price), and receive the same error message.
I appreciate any help, and thanks in advance!
Solution 1:[1]
Try something like this:
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))
If you want to use a for loop on a dictionary you will need to loop over the keys of the dictionary. You can do that by using .keys()
or .items()
functions of a dict. But as you see above, there is no need to loop over the dict keys in your code.
Solution 2:[2]
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.")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | seth egger |