I'm attempting to write a code for class and am having immense trouble with classes. The problem is for us to write a script with one class, Car
, have the speed
method set to '0', and display the speed over a series of ten iterations (calling acceleration
and brake
five times each).
The error I'm getting is:
Traceback (most recent call last): File "C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py", line 36, in <module> main() File "C:/Users/Brown Bear/Documents/Wake Tech/CIS115/Python Documents/Lab14P1.py", line 27, in main my_speed = Car() TypeError: __init__() missing 3 required positional arguments: 'make', 'model', and 'speed'
And this is the example of what the output should be:
Enter model of your car: Prius
Enter make of your car: Toyota
Current speed: 5
Current speed: 10
Current speed: 15
Current speed: 20
Current speed: 25
Current speed: 20
Current speed: 15
Current speed: 10
Current speed: 5
Current speed: 0
And this is my code:
class Car:
def __init__(self, make, model, speed):
self.make = make
self.model = model
self.speed = speed
def accelerate(self):
self.speed += 5
def brake(self):
self.speed -= 5
def get_make(self):
return self.make
def get_model(self):
return self.model
def get_speed(self):
return self.speed
def main():
manuf = input('Enter the car make: ')
mod = input('Enter the car model: ')
my_speed = Car()
for num in range(5):
my_speed.accelerate()
print('Current speed: ', my_speed.get_speed())
for num in range(5):
my_speed.brake()
print('Current speed: ', my_speed.get_speed())
main()
I've spent a ridiculous amount of time on this particular problem and tried various ways, I've also researched similar questions previously asked on here but all are a bit too complex and I just need a simple solution. Any and all help appreciated. Thanks!
When you create an instance of your class you are not passing any arguments:
But your definition says there are 3 required arguments:
So you need to create an instance of Car and pass 3 arguments: make, model and speed.