22.6. Exercises¶
For exercises, you can expand the Tamagotchi game even further. Try these out.
Here’s all the code we just saw for our new and improved game, with a few additions. You can run this and play the game again.
Change the above code to allow you to adopt a Tiger pet (that you’re about to create). HINT: look at the
whichtype
function, and think about what’s happening in the code for that function.Now, modify the code to define a new class,
Tiger
. TheTiger
class should inherit from theCat
class, but its default meow count should be5
, not3
, and it should have an extra instance method,roar
, that prints out the stringROOOOOAR!
.Next, modify the code so that when the
hi
method is called for theTiger
class, theroar
method is called. HINT: You’ll have to call one instance method inside another, and you’ll have to redefine a method for theTiger
class. See the overriding methods section.Now, modify the code to define another new class,
Retriever
. This class should inherit fromLab
. It should be exactly likeLab
, except instead of printing justI found the tennis ball!
when thefetch
method is called, it should sayI found the tennis ball! I can fetch anything!
.Add your own new pets and modifications as you like – remember, to use them in the game, you’ll also have to alter the
whichtype
function so they can be used in game play. Otherwise, you’ll have different classes that may work just fine, but you won’t see the effects in the game, since the code that actually makes the game play is found in the second half of the provided code (look for thewhile
loop!).
22.6.1. Contributed Exercises¶
Copy and paste all the code from the wvu_inheritance_car question.
In the Car class, override the __str()__ class. The new version of the function should return the make, model, and year along with the engine size like “2015 Dodge Journey with 3.6L engine”.
For the car instance you created, set the engine_size property to the size of the car’s engine.
Copy and paste all the code from the wvu_inheritance_override question.
Create a constructor (__init__()) function for the Car class. It should accept the year, make, model, and engine size as parameters. Have your function store the engine size, and then call the Vehicle class’s constructor store the year, make, and model.
Update your code to specify the engine size for your car using its constructor rather than setting it separately afterwards.
Create a new class called Car that inherits from the existing Vehicle class. Add a class variable named engine_size to your new class with a value of None
.
Create an instance of the new Car class to represent a car with the year, make, and model of your choice.
Use your car’s print_vehicle() method to print details about the car.
Think about cases where you might want to use classes with inheritance, like if you have a generic type (beverage) and then a more specific type (pop). Write a parent class, one or more subclasses, and the variables and methods you need to represent and use your items.
Write a new class named Dog to inherit from the Pet class. The new class should have a class variable named breed with a default value of Unknown
. Dog’s constructor must allow users to specify the dog’s name, age, and breed.
To remind yourselves how a class inherits from a super class and how it invokes its constructors please
see Section 22.4. To make this work you need to add at most 4-5 lines.