Ui-Jeen Y.

asked • 03/31/20

Python menu cost calculator problem

The assignment's output should be something like this:

The following is an order for a plain pizza, a ham and anchovy pizza, two "tub"-sized drinks, with a 10%-off coupon:

>>>cost_calculator([], ["ham", "anchovy"], drinks=["tub", "tub"], coupon=0.1)
35.61



I am given the prices for all the items and I should be able to calculate the cost of an order with one function. However, I cannot figure out how to count each of the elements in drinks (for example) and apply the separate prices of the different sizes (small, medium, large, tub).


I made a dictionary:


drinks = {"small":2, "medium": 3, "large": 3.5, "tub":3.75}


But I do not know how to use this to access the prices for each element of drinks (ex. drinks = ["tub", "tub"] would be priced as $7.50).


I tried to do:


for your_drinks in drinks:

your_drinks = drinks.count("small")*2 + drinks.count("medium")*4 + drinks.count("large")*3.5 + drinks.count("tub") * 3.75

return x

But this gives me $3.75, but the answer should be $7.50 for two small drinks (I have no idea why). I hard coded the prices because I did not know how to use the dictionaries logically. I am a beginner in python and I am not very knowledgeable about lists/dictionaries.

1 Expert Answer

By:

Ethan S. answered • 03/31/20

Tutor
5.0 (198)

Ivy League Computer Science and Math Tutor

Ui-Jeen Y.

Thank you so much! I think I just need to be more knowledgeable about that difference.
Report

03/31/20

Ethan S.

You're welcome! It's tricky learning Python, so if there's anything else I can help with just let me know :)
Report

03/31/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.