Python KeyError: I can't figure it out (new to python)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
Python KeyError: I can't figure it out (new to python)
I am in a beginners python class and we are creating a grocery list script. My script is giving me a key error at the line: item_total = int(grocery_history[y].get('number')) * float(grocery_history[y].get('price')). I also think the last couple print statements are wrong as well.
grocery_item =
grocery_history = grocery_item
x = 0
isStopped = False
while not isStopped:
item_name = input("Item name:n")
quantity = input("Quantity purchased:n")
cost = input("Price per item:n")
grocery_item['name'] = item_name
grocery_item['number'] = int(quantity)
grocery_item['price'] = float(cost)
grocery_history[x] = grocery_item.copy()
exit = input("Would you like to enter another item?nType 'c' for continue or 'q' to quit:n")
if exit == 'q':
isStopped = True
else:
x += 1
grand_total = float(0.00)
for y in range(0, len(grocery_history) - 1):
item_total = int(grocery_history[y].get('number')) * float(grocery_history[y].get('price'))
grand_total = float(grand_total) + float(item_total)
print("%d %s @ $%.2f ea $%.2f" %(grocery_history[y]['number'], str(grocery_history[y]['name']), float(grocery_history[y]['price']), float(item_total)))
item_total = 0
finalmessage = ("Grand Total: $:,.2f".format(grand_total))
print(finalmessage)
days_of_week["potato"]
1 Answer
1
I think you are misusing Python dictionaries and lists. The variable grocery_history
could be a list (which stores grocery items right?), so your code could end like:
grocery_history
grocery_item =
grocery_history =
isStopped = False
while not isStopped:
item_name = input("Item name:n")
quantity = input("Quantity purchased:n")
cost = input("Price per item:n")
grocery_item['name'] = item_name
grocery_item['number'] = int(quantity)
grocery_item['price'] = float(cost)
grocery_history.append(grocery_item.copy())
exit = input("Would you like to enter another item?nType 'c' for continue or 'q' to quit:n")
if exit == 'q':
isStopped = True
grand_total = float(0.00)
for y in range(0, len(grocery_history) - 1):
item_total = int(grocery_history[y].get('number')) * float(grocery_history[y].get('price'))
grand_total = float(grand_total) + float(item_total)
print("%d %s @ $%.2f ea $%.2f" % (
grocery_history[y]['number'], str(grocery_history[y]['name']), float(grocery_history[y]['price']), float(item_total)))
item_total = 0
finalmessage = ("Grand Total: $:,.2f".format(grand_total))
print(finalmessage)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
KeyError means that the key is not found in dictionary, like this
days_of_week["potato"]
. I suggest you first break the long line into individual steps. You will get a more specific location of your error. Then, print the dictionary as well as key before that line - this will make it easier to debug.– jurez
37 mins ago