Python KeyError: I can't figure it out (new to python)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

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)





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


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.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs