This script simulates orders inside a pizzeria. I'm trying to create two exceptions. One that warns you when the amount of cheese is too much. One that notifies you when a pizza isn't on the menu.
Why does the line print(tmce, ':', tmce.cheese)
prints too much cheese : 110
,
if in the instruction raise TooMuchCheeseError(pizza, cheese,"too much cheese")
I pass 3
arguments?
shouldn't it print ('margherita', 110, 'too much cheese') : 110
?
Using the tuple args like this print (tmce.args, ':', tmce.cheese)
I get this output :
('too much cheese',) : 110
. Shouldn't the tuple contain three elements?
Here is the code :
class PizzaError(Exception):
def __init__(self, pizza, message):
Exception.__init__(self, message)
self.pizza = pizza
class TooMuchCheeseError(PizzaError):
def __init__(self, pizza, cheese, message):
PizzaError.__init__(self, pizza, message)
self.cheese = cheese
def makePizza(pizza, cheese):
if pizza not in ['margherita', 'capricciosa', 'calzone']:
raise PizzaError(pizza, "no such pizza on the menu")
if cheese > 100:
raise TooMuchCheeseError(pizza, cheese, "too much cheese")
print("Pizza ready!")
for (pz, ch) in [('calzone', 0), ('margherita', 110), ('mafia', 20)]:
try:
makePizza(pz, ch)
except TooMuchCheeseError as tmce:
print(tmce, ':', tmce.cheese)
except PizzaError as pe:
print(pe, ':', pe.pizza)
Output :
Pizza ready!
too much cheese : 110
no such pizza on the menu : mafia