I have an assignment to take in user input to create a grocery list of items, and to eventually output this information along with a grand total onto a receipt.
Example: The user inputs the following grocery items:
{'name':'milk', 'number': 1, 'price': 2.00}, {'name':'eggs', 'number':2, 'price': 3.99}, {'name': 'onions', 'number': 4, 'price':0.79}
.
Then I would want my ouput to be:
1 milk @ $2.99 ea $2.99
2 eggs @ $3.99 ea $7.98
4 onions @ $0.79 ea $3.16
Grand total: $14.13
Instead my ouput is:
4 onions @ $0.79 ea $3.16
4 onions @ $0.79 ea $3.16
4 onions @ $0.79 ea $3.16
Grand total: $9.48
Here is the code I am using. I have a strong feeling my for loop is to blame (they have been the hardest thing to master for me), but I'm not sure why it is only printing the last entry over and over.
grocery_item = {}
grocery_history = []
stop = 'go'
while stop != 'q' :
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)
print("Would you like to enter another item?")
stop = input("Type 'c' for continue or 'q' to quit:\n")
grand_total = []
for grocery_item in grocery_history:
item_total = grocery_item['number'] * grocery_item['price']
grand_total.append(item_total)
print(grocery_item['number'], end=' ')
print(grocery_item['name'],end=' ')
print('@', end=' ')
print('$%.2f' % (grocery_item['price']), end=' ')
print('ea', end=' ')
print('$%.2f' % (item_total))
item_total = 0
sum = sum(grand_total)
print('Grand total: $%.2f' % (sum))