lab13.py pets.py Lab Exercise #13 Assignment overview This lab exercise provides
ID: 3817486 • Letter: L
Question
lab13.py
pets.py
Lab Exercise #13 Assignment overview This lab exercise provides practice with class inheritance in Python. You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each step 1. Examine the file named "lab13.py", which contains a simple test bed for the classes in "petspy", and then execute the test bed. 2. Examine the file named "pets.py", which contains an outline of four classes to support the handling of pets within Python programs. Please note that class "PetError" is a subclass of class "Exception" and is complete as-is. You will complete the remaining three classes as described belowExplanation / Answer
# pastebin link: https://pastebin.com/vkAi9TNn
##
## Class PetError -- complete
##
class PetError( ValueError ):
pass
##
## Class Pet -- not complete
##
class Pet( object ):
def __init__( self, species=None, name="" ):
if species and (species.lower() in ['dog', 'cat', 'horse', 'gerbil', 'hamster', 'ferret']):
self.species = species.title()
self.name = name.title()
else:
raise PetError()
def __str__( self ):
result = "Species of {:s}".format(self.species)
if self.name:
result += ", named {:s}".format(self.name)
else:
result += ", unnamed"
return result
class Dog( Pet ):
def __init__( self, name="", chases="Cats" ):
super(Dog, self).__init__("Dog", name)
self.chases = chases
def __str__( self ):
result = super(Dog, self).__str__()
result += ", chases {:s}".format(self.chases)
return result
class Cat( Pet ):
def __init__( self, name="", hates="Dogs" ):
super(Cat, self).__init__("Cat", name)
self.hates = hates
def __str__( self ):
result = super(Cat, self).__str__()
result += ", hates {:s}".format(self.hates)
return result
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.