Creating the Dog Class (Exercise)
So, the first exercise is literally a review exercise from the associated Python Basics course. So at the end of the course, you ended up with a
Dog class that had this class attribute and a couple of instance attributes.
And your first task is just going to be to re-create this basically, but without looking. So I want you to start from scratch, open up a new file, and then build this
Dog class again with the following specifications: that it should have one class attribute called
.species that has the value
"Canis familiaris" or
"Canis familiaris", which is the Latin name for a dog.
And then each instance of the
Dog class should have a
.name and an
.age. Also create a readable representation for a
Dog instance, which means that if you print one instance of the
Dog class, then it should give a nicely readable string. And finally, you should also create an instance method called
.speak() that takes a sound as an argument and prints a sentence that the dog says, the specific sound that you entered. So, for example, it could look like this.
Create an instance of
Dog that you’re naming
philo here, and you’re passing in a name and an age. And then you should be able to print
philo and get out string that says
12 years old. And you should be able to call
.speak() on the
philo instance and pass it a string. This, this is how dogs speak in German. So here it’s saying,
"Wau". And then
Wau. So that’s the
Dog class you should model.
So before you move on to the next lesson where I’ll also build it out, do it yourself. Start from scratch, open up a new file, and then build the
Dog class and make sure that it works as expected.
Become a Member to join the conversation.