Skip to content

Car Database

  • Project
  • 1 hint
  • 6 of 7 in this chapter

Assignment

Let’s create a program that has information about a few car models in it, and asks the user to pick a car, after which, the program proceeds to describe that car.

The car’s information should be stored as objects of classes, rather than just variables. Also, you’ll need to use the input() function to allow the user to provide input, as in earlier projects.

The program should look something like this when run:

Text
Model Name: 
And when input is given…

Model Name: Merc E 330

About the Merc E 330:
The Merc E 330 is a(n) sedan with 400hp and costs $40000. 
Text
Model Name: Caterham A5

About the Caterham A5:
The Caterham A5 is a(n) sports car with 700hp and costs $75000.

Hints

Hint 1h

a

Solution

Python
class Vehicle:
    def __init__(self, name, car_type, power, cost):
        self.name = name
        self.car_type = car_type
        self.power = power
        self.cost = cost
    def description(self):
        desc_str = "The " + self.name + " is a(n) " + self.car_type + " with " + str(self.power) + "hp and costs $" + str(self.cost) + "."
        return desc_str

car1 = Vehicle("Merc E 330", "sedan", 400, 40000)
car2 = Vehicle("Caterham A5", "sports car", 700, 75000)

choice = input("Model Name: ")

if choice == car1.name:
    print(car1.description())
elif choice == car2.name:
    print(car2.description())
else:
    print("That car is not registered in the system")

Give the project a try first.