Question: Python 2. Car Class Python 3.6.3 with IDLE.. need help code does not work Write a class named Car that has the following data attributes:

Python 2. Car Class Python 3.6.3 with IDLE.. need help code does not work

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

_ _year_model (for the cars year model)

_ _make (for the make of the car)

_ _speed (for the cars current speed)

The Car class should have an _ _init_ _ method that accepts the cars year model and make

as arguments. These values should be assigned to the objects _ _year_model and _ _make

data attributes. It should also assign 0 to the _ _speed data attribute.

The class should also have the following methods:

accelerate

The accelerate method should add 5 to the speed data attribute each time it is called.

brake

The brake method should subtract 5 from the speed data attribute each time it is called.

get_speed

The get_speed method should return the current speed.

Next, design a program that creates a Car object and then calls the accelerate method

five times. After each call to the accelerate method, get the current speed of the car and

display it. Then call the brake method five times. After each call to the brake method, get

the current speed of the car and display it.

Expert Answer

 Python 2. Car Class Python 3.6.3 with IDLE.. need help codeRjExtreme answered this

Was this answer helpful?

0

0

109 answers

Notice:

Use this link if indent fails in chegg: https://repl.it/OGMa/0

Execute it on your pc and send me comments if anything goes wrong. **Explanation for each line in code comments.** Cheers, have a great day mate!

Code:

""" Car Class """

class Car(object): # Constructor, takes arguments and constructs the class def __init__(self, year, make): self.__year = year # car's year self.__make = make # make of the car self.__speed = 0 # current speed

def accelerate(self): """ Accelerate car. """ self.__speed += 5

def brake(self): """ Decelerate car. """ self.__speed -= 5

def get_speed(self): """ Get current speed of car. """ return self.__speed

def main(): # create a Car object car = Car("1990 Laser", "Plymouth") # calls the accelerate method print "Accelerating . . ."

for i in xrange(5): car.accelerate() print car.get_speed()

# calls the brake method five times. print "Decelerating . . ."

for i in xrange(5): car.brake() print car.get_speed()

if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!