Ui-Jeen Y.

asked • 04/01/20

Cost calculator: how to count strings in a positional argument in Python

The goal is to calculate the cost of a order at a pizza shop. For example:


cost([ ], ["pepperoni"], ["ham", "anchovy], drinks = ["small"])


should calculate the price of one plain pizza, one pizza with pepperoni toppings, one pizza with ham and anchovy toppings, and one drink (3 pizzas, one drink in total). Right now, I get the price of 3 pizzas and one drink minus the price of the 3 toppings (pepperoni, ham, anchovy).


The pizzas are arbitrary positional arguments but the drinks are named orders (keyword arguments). I was able to figure out how to count the number of positional arguments (pizzas) but I cannot figure out how to account for the price of the toppings.


I am confused about how to deal with positional arguments in this case: pizza. I am mainly confused about how to account for the toppings. I thought *pizza would allow me to access cost(pizza) as a list, but it does not work.


This is what I have so far.

def cost(*pizza, drinks):
for your_drinks in drinks:
drinks_cost = drinks.count("small")*2
pizza_cost = len(pizza)*13 #plain pizza costs $13

#this for loop does not behave the way I want it to
#how can I count the toppings in the argument pizza?
for toppings in pizza:
toppings = pizza.count("pepperoni")*1 + pizza.count("anchovy")*2 + /
pizza.count("ham")*1.5

total = drinks_cost + toppings + pizza cost
return total


Thank you so much!

1 Expert Answer

By:

Ethan S. answered • 04/03/20

Tutor
5.0 (198)

Ivy League Computer Science and Math Tutor

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.