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.")
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
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.")
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images