Classes-Exceptions Lesson in Python course


#1

I’m stumped on this project even though the reviewer says the code is right and allows me to move to the next lesson. At the top of the code I define the class OutOfStock(Exception):. When the code runs, it prints the statement in the class definition. The exception never executes when the condition is met within the body of the program.

Here’s the project - Classes - Exceptions
www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/inheritance-and-polymorphism/exercises/exceptions

Here’s the code

Define your exception up here

class OutOfStock(Exception):
print(“Exception called”)

print("\n an exception has been made")

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:
print(self.stock[color])
raise OutOfStock
else:
self.stock[color] = self.stock[color] - 1

candle_shop = CandleShop({‘blue’: 6, ‘red’: 2, ‘green’: 0})
print(candle_shop.stock)
candle_shop.buy(‘blue’)
print(candle_shop.stock)

This should raise OutOfStock:

candle_shop.buy(‘green’)
print(candle_shop.name)

Here’s the output - with error
Exception called
{‘red’: 2, ‘blue’: 6, ‘green’: 0}
{‘red’: 2, ‘blue’: 5, ‘green’: 0}
0
Traceback (most recent call last):
File “script.py”, line 26, in
candle_shop.buy(‘green’)
File “script.py”, line 17, in buy
raise OutOfStock
main.OutOfStock


#2

@oduffy, what page was that new users are supposed to read?


#3

I don’t understand your question.


#4

I’m working through this one at the moment and while I’m able to continue moving forward in the course, there seems to be something wrong with this particular exercise. Just typing ‘Raise’ doesn’t work if you test it for an error (uncomment buy Green) with the solution code. I believe that it needs a Try / Except block to function properly. I hope the team will examine the code.