Question: (ANSWER USING PYTHON 3.6) In this homework you will design a class Dog and save it in a file named dog . py . (

(ANSWER USING PYTHON 3.6)

In this homework you will design a class Dog and save it in a file named dog.py. (Important hint: when you change the file dog.py the changes will not be reflected in an old shell, even if you import dog again. Start a new shell to test your changes.)

Problem 1. Write a class definition line and a one line docstring for the class Dog. Write an __init__ method for the class Dog that gives each dog its own name and breed. Make sure that you test this on a successful creation of a dog object. For example,

>>> import dog >>> sugar = dog.Dog('Sugar', 'border collie') >>> sugar.name Sugar >>> sugar.breed border collie Problem 2. Add a list attribute tricks to each dog and initialize it in __init__ to the empty list. (The user does not have to supply a list of tricks when creating a dog.) Make sure that you test this successfully.

>>> sugar.tricks []

Problem 3. Write a method teach as part of the class Dog. The method teach should add a passed string parameter to tricks and print a message that the dog that knows the trick.

>>> sugar.teach('frisbee') Sugar knows frisbee

Problem 4. Write a method knows as part of the class Dog. The method knows should check whether a passed string parameter is in the dogs list of tricks, print an appropriate message and return True or False.

>>> sugar.knows('frisbee') Yes, Sugar knows frisbee True >>> sugar.knows('arithmetic') No, Sugar doesn't know arithmetic False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!