Mar-17-2020, 11:43 PM
I need to write a code that will multiply the price of each item with its respective quantity and then return whichever dictionary has the highest total. In this case, Chocolate should be returned since it has the highest total (.75 * 9 = 6.75).
shopping_cart = { "tax": .08, "items": [ { "title": "orange juice", "price": 3.99, "quantity": 1 }, { "title": "rice", "price": 1.99, "quantity": 3 }, { "title": "beans", "price": 0.99, "quantity": 3 }, { "title": "chili sauce", "price": 2.99, "quantity": 1 }, { "title": "chocolate", "price": 0.75, "quantity": 9 } ]}I believe I need to create an empty dictionary and use it to compare each item but I'm not sure. I've written the code below but I've hit a wall now. Any guidance would be appreciated.
def most_spent_on_item(d): NewDict="title": " ", "price": 0, "quantity": 0 for i in d['items']: i['price'] * i['quantity']