I am doing a python assignment where I compute the cost of an order at a pizza shop:
You work at a pizza restaurant, which is starting to accept orders online. You need to provide a python function that will accept an arbitrary order as its arguments, and compute the correct price for the order.
Your cost-calculator function should have four arguments:
- pizzas
- drinks
- wings
- coupon
A single pizza order is formed as a list of toppings. For example
- A pizza with no toppings (other than cheese and sauce is: [] (an empty list)
- A pizza with pepperoni and a double order of olives is : ["pepperoni", "olives", "olives"]
An arbitrary number of pizzas may be ordered, including no pizzas as all
Drinks come in as a named order (i.e. a keyword argument 'drinks'). If drinks are ordered, they are specified as a list of sizes (possible sizes: "small", "medium", "large", "tub"). For example, drinks=["small", "small", "large"]would indicate an order including two small drinks and a large drink.
Wings come in as a named order as well (keyword argument 'wings'). If wings are ordered, they are specified as a list of integers (possible sizes: 10, 20, 40, 100). For example, wings=[20]would indicate a single order of 20-piece wings.
A coupon may be specified as the keyword argument 'coupon'. It is will be a single floating point number between 0 and 1. This indicates the fraction of the pre-tax price that is to be subtracted. For example coupon=.25would indicate a 25%-off coupon.
A 6.25% tax is applied to every order. The tax is computed on the total cost of the order before a coupon is applied
Round the price to the nearest cent, using the built-in function round. round(x, 2)will round xto two decimal places.
Prices
The prices are as follows:
Pizza
- $13.00
Toppings
- pepperoni : $1.00
- mushroom : $0.50
- olive : $0.50
- anchovy : $2.00
- ham : $1.50
Drinks
- small : $2.00
- medium : $3.00
- large : $3.50
- tub : $3.75
Wings
- 10 : $5.00
- 20 : $9.00
- 40 : $17.50
- 100 : $48.00
Examples
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
This order consists only of a small drink.
>>> cost_calculator(drinks=["small"]) 2.12
This is an order of two plain pizzas, a pizza with double-pepperoni, an order of a 10-piece and a 20-piece wings, and a small drink.
>>> cost_calculator([], [], ["pepperoni", "pepperoni"], wings=[10, 20], drinks=["small"]) 60.56
Details
You can assume that the front-end of the website will never pass your function erroneous orders. That is, you will never receive orders for items that do not exist nor items that contain typos.
Consider defining individual functions responsible for computing the cost of the pizzas, drinks, and wings, respectively. Have cost_calculatorinvoke these internally. Alternatively, you can read ahead about dictionaries and make nice use of these in this problem.
Our cost_calculatorsignature is empty. Part of the assignment is to come up with the correct function signature!
The code I have so far is here:
def cost_calculator(pizzas=[],drinks=[],wings=[],coupon=0.0):
toppings = {"pepperoni":1,"mushroom":0.5,"olive":0.5,"anchovy":2,"ham":1.5}
d = {"small":2,"medium":3,"large":3.5,"tub":3.75}
w = {10:5,20:9,40:17.5,100:48}
cost = 13 * len(pizzas)
for i in pizzas:
for j in toppings:
cost = toppings[j]
for i in drinks:
for i in d:
cost = d[i]
for i in wings:
for i in w:
cost = w[i]
disc = 0
if coupon != None:
disc = cost * coupon
else:
disc = 0
cost = cost * 0.0625
cost -= disc
return cost
My issue is that when it comes to calculating the cost, it always outputs 0 instead of the expected number:
StudentError:
Calling
student_function([])
produced an incorrect result.
Expected:
13.81
Got:
0.0
I tried removing some of the calculations with the cost, such as removing the discount subtraction to see if that was the issue, but it still ends up being zero. I looked into other posts on the subreddit and I am still unable to debug this. All help is greatly appreciated!!
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...