My Python code has an attribute error wherein 'Car' has no attribute 'accelerate' attached to it, but it is clearly defined in my code. Can you help please? My code is below. class Car: def __init__(self, model_year, make): self.__model_year = model_year self.__make = make self.__speed = 0 def accelerate(self): self.__speed += 3 def brake(self): self.__speed -= 3 def get_speed(self): return self.__speed # Make Car object my_subaru = Car(2015, "Subaru Forester") # Accelerate car 5x and show speed after each acceleration for _ in range(5): my_subaru.accelerate() the_speed = my_subaru.get_speed() print(f"Current Speed is: {the_speed} mph.") # Brake the car 5x and show speed after each brake for _ in range(5): my_subaru.brake() the_speed = my_subaru.get_speed() print(f"Current Speed is: {the_speed} mph.")

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 5PE
icon
Related questions
Question

My Python code has an attribute error wherein 'Car' has no attribute 'accelerate' attached to it, but it is clearly defined in my code. Can you help please? My code is below.

 

class Car:
    def __init__(self, model_year, make):
        self.__model_year = model_year
        self.__make = make
        self.__speed = 0


        def accelerate(self):
            self.__speed += 3


        def brake(self):
            self.__speed -= 3


        def get_speed(self):
            return self.__speed


# Make Car object


my_subaru = Car(2015, "Subaru Forester")


# Accelerate car 5x and show speed after each acceleration


for _ in range(5):
    my_subaru.accelerate()
    the_speed = my_subaru.get_speed()
    print(f"Current Speed is: {the_speed} mph.")


# Brake the car 5x and show speed after each brake

for _ in range(5):
    my_subaru.brake()
    the_speed = my_subaru.get_speed()
    print(f"Current Speed is: {the_speed} mph.")

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,