FAQ: Learn Python: Inheritance and Polymorphism - Exceptions


#1

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!


#2

Hi! I was wondering why this code doesn’t work, anyone can help?

  def buy(self, color):    
    if self.stock[color] == 0:
      raise OutOfStock
    else:
      self.stock[color] = self.stock[color] - 1

Thanks in advance!


#3

Hey! I know this was written a while ago, and I’m not sure if this was what was happening on your end, but I wanted to mention…for others that might be like me specifically…

I wrote this exact code for the exercise and kept getting errors in the console. Then I realized that I was supposed to be getting an error…because we’re raising an error…haha.


#4

I’ve written the exact same thing. Stock shouldn’t be able to be negative anyways, so checking for 0 is fine.
Also, we are not raising an exception from outside of the buy method, as the hint suggests. I don’t understand why this code is not valid.