Trouble with Pizza Shop function [on hold]
Trouble with Pizza Shop function [on hold]
This question was answered before but the answer only worked for one example. I reposted so it could get more traction.
I'm having problems with my code for a pizza shop problem,Here are the instructions.
Below are of the example inputs that the grader would pass through the function
>>>cost_calculator(, ["ham", "anchovy"], drinks=["tub", "tub"], coupon=0.1)
35.61
>>> cost_calculator(drinks=["small"])
2.12
>>> cost_calculator(, , ["pepperoni", "pepperoni"], wings=[10, 20], drinks=["small"])
60.56
represents a pizza, and the second ["ham", "anchovy"] is a pizza with toppings.
One of the main things that confuses me is how I would represent as pizza, which costs $13. I have a dictionary placeholder, but that doesn't work beause isn't hashable. Another problem I'm facing is how I would deal with the toppings, that are somehow contained inside of the list. My function signature is also incorrect, I've had that issue multiple times in the past, if somebody could point me in the direction of a resource explaining them, I'd be very grateful. I'm fairly confident in my dictionary usage however but idk.
def cost_calculator(x,wings,drinks,coupon):
total_cost = 0
x = {:13}
Price_of_drinks = {"small": 2.00, "medum":3.00,"large":3.50,"tub":3.75}
Price_of_wings = {10:5.00,20:9.00,40:17.50,100:48.00}
Price_of_toppings = {"pepperoni":1.00,"mushroom":0.50,"olive":0.50,"anchovy":2.00,"ham":1.50}
if x in iter
total_cost += x[]
if "pepperoni" in x
total_cost += 1.00
if "mushroom" in x
total_cost += 0.50
if "olive" in x
total_cost += 0.50
if "anchovy" in x
total_cost += 2.00
if "ham" in x
total_cost += 1.50
if wings in iter
total_cost += Price_of_wings['wings']
if drinks in iter:
total_cost += Price_of_drinks['drinks']
if wings in iter:
total_cost += Price_of_wings['wings']
if coupon in iter
total_cost= total_cost - (total_cost*coupon)
total_cost *= 1.0625
round(total_cost,2)
return total_cost
I'm just looking for a pointer in the right direction, and be sure to edit if you feel my questions are off. I understand that my knowledge is very basic, so a pointer means a lot.
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
First of all, your question must be self-contained: no off-site postings. Next, ask a specific question. I can't tell how specific this might be, as your main problem description isn't included. I'm unclear what you're trying to do with the first parameter, because you immediately overwrite its value with an invalid dict.
– Prune
2 days ago
I gave a quick shot at running your code to get some idea of what you're trying to do. You have quite a few syntax errors. It seems that you've written quite a bit more code than you can yet handle at one shot. Please back up, fix the initial problems, and advance a few lines at a time. When you have a Minimal, complete, verifiable example, please post again. The current posting has too many problems in parallel for this to be a viable Stack Overflow question.
– Prune
2 days ago
3 Answers
3
You would instead write a function pizza_cost
that takes a pizza and returns its price. You can make the base price of a pizza a constant in your program.
pizza_cost
PIZZA_BASE_COST = 13
toppings = {
"pepperoni": 1.0,
"mushrooms": .5,
"olives": .5,
"anchovies": 2.0,
"ham": 1.5,
}
def pizza_cost(pizza):
price = PIZZA_BASE_COST
for topping in pizza:
price += toppings[topping]
return price
You can specify optional arguments by giving them a default value. In this case, it's easiest to make the default values empty tuples (not lists) for the two parameters that expect iterable arguments.
def cost_calcultor(pizzas, drinks=(), wings=(), coupon=0)
total_price = 0
for pizza in pizzas:
total_price += pizza_cost(pizza)
...
The comments on your question give you a good idea of everything wrong with your question. However, I think there are some pointers that can help you progress with your assignment.
- Variable number of parameters.
The number of arguments to your function is not constant, since there can be multiple pizzas or none at all. Hence your function definition is wrong. You need *args
and **kwargs
special syntax, and you can read about it here. I will leave it upto you to implement it.
*args
**kwargs
- Empty list
You can have a base cost, and the cost of toppings is added on it. Once you parse it is an empty list, you can simply return the base cost, and not even go through the toppings. Something like this:
cost = 13 # base cost
for x in pizzas:
if (not x): # Check for empty list
continue
for topping in x:
# use your dictionary to add the corresponding value of each topping
cost = cost + price_of_toppings[topping]
This is just a rough idea, use it to build on. Quite likely you will run into some trouble. When you have narrowed your problem and have some parts working, then you can post a question keeping in mind the above comments.
Good luck!
I suggest you look into *args and **kwargs, which is how, in python, you unpack arbitrary amount of arguments(args) and keyword arguments(kwargs). Here is a modified version of your script that calculates pizza prices only, just to get you started on the concept. Also try to use shorter variable names :). Print func at the bottom is for just for testing purposes.
def cost_calculator(*pizzas, **extras):
total_cost = 0
drinks_prices = {"small": 2.00,
"medum": 3.00,
"large": 3.50,
"tub": 3.75}
wings_prices = {10: 5.00,
20: 9.00,
40: 17.50,
100: 48.00}
toppings_prices = {"pepperoni": 1.00,
"mushroom": 0.50,
"olive": 0.50,
"anchovy": 2.00,
"ham": 1.50}
for pizza in pizzas:
if pizza == :
total_cost += 13
elif pizza != :
total_cost += 13
for top in pizza:
if top == "pepperoni":
total_cost += 1.00
if top == "mushroom":
total_cost += 0.50
if top == "olive":
total_cost += 0.50
if top == "anchovy":
total_cost += 2.00
if top == "ham":
total_cost += 1.50
return total_cost
some_cool_variable_name = cost_calculator(, ["ham", "anchovy"])
print(some_cool_variable_name)
This is extremely bare-bones in regards to the case study. Make sure you add more functionality to make it more usable and work as expected. For example: what happens when you enter no pizzas (just buying drinks or something) should be handled properly.
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic and how to ask apply here.
– Prune
2 days ago