'Cannot find a way to prove functions correctly on pizza shop assignment
drinks = []
wings = []
coupon = 0.1
def cost_calculator(p, drinks=None, wings=None, coupon=None):
receipt = 0
receipt = receipt + pizza_calc(pizzas)
receipt = receipt + drinks_calc(drinks)
receipt = receipt + wings_calc(wings)
receipt = receipt*0.0625
receipt = receipt*coupon
receipt = receipt(receipt, 2)
def pizza_calc(pizzas):
p = {[]:13}
total += p[[]]
for p in pizzas:
if pepperoni in p:
total += 1
if mushroom in p:
total += 0.5
if olive in p:
total += 0.5
if anchovy in p:
total += 2
if ham in p:
total += 1.5
return(p)
def drinks_calc(drinks):
d = 0
total = 0
for d in drinks:
if d == "small":
total += 2
if d == "medium":
total += 3
if d == "large":
total += 3.5
if d == "tub":
total += 3.75
return(d)
def wings_calc(wings):
w = 0
total = 0
for w in wings:
if w == [10]:
total += 5
if w == [20]:
total += 9
if w == [40]:
total += 17.5
if w == [100]:
total += 48
return(w)
The goal for this assignment is figuring out how much the pizzas and other foods cost. I got stuck when inputting cost_calculator([], ["ham", "anchovy"] , coupon=0.1) as separate cell and find the cost of what is represented as one plain pizza, one pizza with ham and anchovy, with a 0.1% off coupon.
These links explain what exactly the assignment is [1]: https://i.stack.imgur.com/MiWB0.png [1]: https://i.stack.imgur.com/ZG0sm.png [1]: https://i.stack.imgur.com/DVbVM.png
Solution 1:[1]
Well, Lillian, there are several things you want to think about changing to make the code work, and to get the correct calculation.
One tricky part of that exercise is the ability to pass multiple, but unknown number of pizzas, to the cost calculator. My approach to that is to use the *args method to pass several pizzas in the way specified in the problem statement.
A few other points...
- you want to return the total from each of the individual calculator functions
- you want to treat toppings (like ham) as strings in the pizza calculator if statements
- well, and a few other things.
You can try this example code that modified several things from your original attempt
def cost_calculator(*pizzas, drinks=None, wings=None, coupon=None):
receipt = 0
receipt += pizza_calc(pizzas)
if drinks is not None:
receipt += drinks_calc(drinks)
if wings is not None:
receipt += wings_calc(wings)
tax = receipt * 0.0625
discount = receipt * coupon
receipt = receipt - discount + tax
receipt = round(receipt, 2)
return(receipt)
def pizza_calc(pizzas):
total = 13 * len(pizzas)
if len(pizzas):
for p in pizzas:
if 'pepperoni' in p:
total += 1
if 'mushroom' in p:
total += 0.5
if 'olive' in p:
total += 0.5
if 'anchovy' in p:
total += 2
if 'ham' in p:
total += 1.5
return(total)
def drinks_calc(drinks):
total = 0.0
for d in drinks:
if d == "small":
total += 2
if d == "medium":
total += 3
if d == "large":
total += 3.5
if d == "tub":
total += 3.75
return(total)
def wings_calc(wings):
total = 0.0
for w in wings:
if w == [10]:
total += 5
if w == [20]:
total += 9
if w == [40]:
total += 17.5
if w == [100]:
total += 48
return(total)
if __name__ == "__main__":
p = ["ham", "anchovy"]
drinks = ["small"]
wings = []
coupon = []
total_bill = cost_calculator([], ["ham", "anchovy"], drinks = ["tub", "tub"], coupon = 0.1)
print(total_bill)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Akida |
