Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I create this python scrypt? Your script must contain the definition of t

ID: 3829262 • Letter: H

Question

How do I create this python scrypt?

Your script must contain the definition of the class Pets with hidden fields containing

name

type

age

The constructor should take one argument, the name of the pet.

It should set the type to the empty string and the age to None

The class should have the following public functions

set_type

set_age

get_name

get_type

get_age

older

set_type should print an error message if the type is not 'cat' or 'dog'.

get_age should use a conversion function to set the age attribute to an integer.

It should catch the exception cause by calling set_age with an argument that cannot be turned into an integer and print an error message when this happens.

It should also have the following magic methods

__str__

__sub__

The __sub__ method should return the integer you get from subtracting the age of another Pet object from the age of the object.

older should print the messages you will see below in the testing code.

Testing

Add the following test code to your scrypt

Your output should look something like this

Explanation / Answer

class Pet:
def __init__(self, name):
self.name = name
self.type = ""
self.age = None
  
def get_name(self):
return self.name
def set_type(self, type):
if type != 'cat' and type != 'dog':
print(str(type) + " is not a valid type")
return
self.type = type
def set_age(self,age):
try:
self.age = int(age)
except:
print(str(age) + " cannot be converted into an integer")
def get_age(self):
return self.age
def get_type(self):
return self.type
def __str__(self):
return self.name + ", " +self.type
def __sub__(self, other):
if self.age and other.age:
return self.age - other.age
def older(self, other):
diff = self - other
if diff > 0:
print(self.name + " is older than " + other.name)
elif diff < 0:
print(self.name + " is younger than " + other.name)
else:
print(other.name + " and " + self.name + " are the same age")
  
if __name__ == '__main__':
p1 = Pet('Henry')
p1.set_type('dawg')
p1.set_type('dog')
p1.set_age('seven')
p1.set_age('7')
print(p1.get_name())
print(p1.get_type())
print(p1.get_age())
print(p1)
  
p2 = Pet('Felix')
p2.set_age(2)
p2.older(p1)
p1.older(p2)
  
p3 = Pet('Angus')
p3.set_age(7)
p3.older(p1)
p1.older(p3)

# code link: https://paste.ee/p/dn4Q7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote