# Define your exception up here:
class OutOfStock(Exception):
pass
# Update the class below to raise OutOfStock
class CandleShop:
name = "Here's a Hot Tip: Buy Drip Candles"
def __init__(self, stock):
self.stock = stock
def buy(self, color):
if self.stock[color] > 0:
self.stock[color] = self.stock[color] - 1
else:
raise OutOfStock
candle_shop = CandleShop({'blue': 6, 'red': 2, 'green': 0})
candle_shop.buy('blue')
# This should raise OutOfStock:
candle_shop.buy('green')
I don’t understand why this keeps getting refused as an answer for the second step in this exercise. I’m stuck now and can’t progress in the course because of this nonsense. I even tried swapping the order to first raise the exception if stock equals zero, else do the buy method, but also doesn’t get accepted.
2.
Have CandleShop raise your OutOfStock exception when CandleShop.buy() tries to buy a candle that’s out of stock.
This is what it shows when I run this code, and I can’t make any sense out of it. Adding dunder main. in front of the exception name makes no difference whatsoever:
Traceback (most recent call last):
File "script.py", line 22, in <module>
candle_shop.buy('green')
File "script.py", line 13, in buy
raise OutOfStock
__main__.OutOfStock
The following is the given solution to the exercise. Why is this accepted, and not my code?
# Define your exception up here:
class OutOfStock(Exception):
pass
# Update the class below to raise OutOfStock
class CandleShop:
name = "Here's a Hot Tip: Buy Drip Candles"
def __init__(self, stock):
self.stock = stock
def buy(self, color):
if self.stock[color] < 1:
raise OutOfStock
self.stock[color] = self.stock[color] - 1
candle_shop = CandleShop({'blue': 6, 'red': 2, 'green': 0})
candle_shop.buy('blue')
# This should raise OutOfStock:
# candle_shop.buy('green')
Your ‘buy’ method looks correct, but look again at the comment toward the bottom. When you comment out the last line (candle_shop.buy(‘green’)), this is the line of the program were we want to use a try/except clause to call the Exception class.
I had the same problem, even if I write the same solution given by the exercise with the only change is giving the command candle_shop.buy(‘green’), instead of hiding it with the # sign. So the only way to pass this exercise is just by hiding the last command line as the sln gives.
When we raise/ throw an exception should we place a return keyword afterwards? Or will control flow always be sent back to the place at which the function was called?
It would depend upon whether you want to handle the exception or throw it. No return would be needed in the former case if you let Python return None to the caller.
Yeah, I suppose I just assumed trace back errors are something to avoid because they stop the execution of the rest of your code. Wouldn’t a try except statement be better in this case as you could avoid a trace back?
Custom exception handling is a way to prevent fatal errors, but it is probably best to let the errors happen so we can identify them and their cause. Anticipating all the possible things that can go wrong could derail our creative focus. Write code that works in a best case scenario, then tighten the screws on each edge case with one’s own error handling. In many cases we don’t need to use try..except if our own handing of errors is successful and there are no edge cases sneaking past.
When we can anticipate edge cases that will raise an exception and cannot handle it ourselves without code bloat then except that specific error, and let the Exception class handle all the others. We can still echo the error message without killing the program. Eventually we will come to trust the debugged code once we address all the possible errors that can be raised. Use exceptions as both a learning tool and a programming tool.
Codecademy accepts my following answer, but for my curiosity, I commented the last line for Green purchasing and still the error is raised. Isn’t the exception error supposed to be raised only for items out of stock?
class OutOfStock(Exception):
print("We ran out of stock.")
# Update the class below to raise OutOfStock
class CandleShop:
name = "Here's a Hot Tip: Buy Drip Candles"
def __init__(self, stock):
self.stock = stock
def buy(self, color):
print(self.stock[color],color)
if self.stock[color]<=0:
raise OutOfStock
else:
self.stock[color] = self.stock[color] - 1
print(self.stock[color],color)
candle_shop = CandleShop({'blue': 6, 'red': 2, 'green': 0})
candle_shop.buy('blue')
# This should raise OutOfStock:
#candle_shop.buy('green')
#Output:
#We ran out of stock.
#6 blue
#5 blue
Hi,
I am stuck in example problem of Refrigerator. Below is the program:
class KitchenException(Exception):
"""
Exception that gets thrown when a kitchen appliance isn't working
"""
class MicrowaveException(KitchenException):
"""
Exception for when the microwave stops working
"""
class RefrigeratorException(KitchenException):
"""
Exception for when the refrigerator stops working
"""
def get_food_from_fridge():
if refrigerator.cooling == False:
raise RefrigeratorException
else:
return food
def heat_food(food):
if microwave.working == False:
raise MicrowaveException
else:
microwave.cook(food)
return food
try:
food = get_food_from_fridge()
food = heat_food(food)
except KitchenException:
food = order_takeout()
I am finding difficulty in understanding this program execution.
When I am running this program in my own console, showing below error -
if refrigerator.cooling == False:
NameError: name 'refrigerator' is not defined
What is expected? and I feel this program is incomplete.
Eagerly waiting for inputs.
Thanks
Hi,
I am continuing to my own message. Done some modifications to the program.
class KitchenException(Exception):
pass
class MicrowaveException(KitchenException):
pass
class RefrigeratorException(KitchenException):
pass
class refrigerator:
cooling = True
class microwave:
working = True
def cook(food):
return [item for item in food]
def get_food_from_fridge(food = []):
if refrigerator.cooling == False:
raise RefrigeratorException
else:
return food
def heat_food(food):
if microwave.working == False:
raise MicrowaveException
else:
microwave.cook(food)
return food
food = get_food_from_fridge(['spinach', 'tomato'])
food = heat_food(food)
print(food)
#food = order_takeout()
I am getting below error and also does not know how to utilize order_takeout(). Please help.
File "E:\Python_Classes\kitchen_exception.py", line 70, in <module>
food = heat_food(food)
File "E:\Python_Classes\kitchen_exception.py", line 66, in heat_food
microwave.cook(food)
AttributeError: type object 'microwave' has no attribute 'cook'
Eagerly waiting for any reply/inputs.
Many thanks in advance.