Write a class named Car that uses the following data attributes:
The following set of codes works efficiently:
# The car class
class Car():
"""Describe a car model"""
def __init__(self, year, model, make):
"""Initialize attributes to describe a car."""
self.year = year
self.model = model
self.make = make
self.speed = 0
def accelerate(self):
"""Increase speed by 5"""
self.speed += 5
def brake(self):
"""Lower speed by 5"""
self.speed -= 5
def get_speed(self):
return self.speed
def main():
"""Accessing attributes and calling methods."""
my_car = Car(2020, 'BMW', 'X5')
print("Accelerate")
my_car.accelerate()
print("Accelerate")
my_car.accelerate()
print("Accelerate")
my_car.accelerate()
print(f"Current speed is {my_car.get_speed()}")
print("Brake")
my_car.brake()
print(f"Current speed is {my_car.get_speed()}")
main()
Comments
Leave a comment