Answer to Question #319352 in Python for S`E

Question #319352

Write a class named Car that uses the following data attributes:


1
Expert's answer
2022-03-28T08:03:16-0400

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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS