Ask coding questions

← Back to all posts
Could you please help fix the following code in Python?
marinazhiv (0)

shopping_cart = ["celery", 4, 9, "beef", 15, 3, "eggs", 2, 12, "bread", 4, 1]
item = string()
for item, cost, qty in shopping_cart:
unit = cost/qty
unit = round (unit, 2)
print(item[0], "has a unit cost of $", unit)

Answered by JoshDaBosh (77) [earned 5 cycles]
View Answer
Commentshotnewtop
JoshDaBosh (77)

you forgot to group the ingredients together in shopping_cart like so:

shopping_cart = [["celery", 4, 9], ["beef", 15, 3], ["eggs", 2, 12], ["bread", 4, 1]]
ash15khng (509)

@JoshDaBosh You should use a dict for that.