Back
Close

Python Classes Evaluation

waltervr
3,080 views

Exercise 1: Create a Car Class

Create a Car class with two instance attributes:

  • Color, which stores the name of the car’s color as a string.
  • Mileage, which stores the number of miles on the car as an integer.
  • Then instantiate two Car objects—a blue car with 20,000 miles and a red car with 30,000 miles—and print out their colors and mileage.

Your output should look like this:

The blue car has 20,000 miles.
The red car has 30,000 miles.

Exercise 2: Create an Animal Class

Create an Animal class with one instance attribute and one method:

  • The instance attribute will be the name, which stores the name of the animal as a string.
  • The method should return the sound of the animal, which returns the following string: "{variable_sound}, I'm {animal_name}! {variable_sound}"
  • Then create two subclasses that inherits from the Animal class, and assign a sound to the instance attribute.
  • Finally, create an instance of each of the subclasses and print the sound of each of them.

Your output should look like below, considering you have a cow with sound Moooo and name Milky White:

Moooo I'm Milky White! Moooo
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io