FAQ: Learn Python: Inheritance and Polymorphism - Exceptions

This community-built FAQ covers the “Exceptions” exercise from the lesson “Learn Python: Inheritance and Polymorphism”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Exceptions

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: How can I catch a raised exception and pass the lesson?

A post was split to a new topic: What’s wrong with my code?

A post was split to a new topic: I’m trying to practice the example but it isn’t working?

3 posts were split to a new topic: How can I print the name in the class?

# 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')
1 Like

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.