Reputation: 79
Traceback (most recent call last): File "H:\Documents\itp100-softwaredesign\Assinginments\Project2\run_car.py", line 36, in main() File "H:\Documents\itp100-softwaredesign\Assinginments\Project2\run_car.py", line 23, in main my_car.accelerate() File "H:\Documents\itp100-softwaredesign\Assinginments\Project2\car.py", line 28, in accelerate speed + 5 TypeError: unsupported operand type(s) for +: 'Car' and 'int'
This is the error im getting. this is my code and my class. i need to add 5 to the speed 5 times with accelerate method and then -5 from the speed with the brake method. any advise would be helpful.Thanks
class Car:
def __init__(self, year, make, speed):
self.__year_model = year
self.__make = make
self.__speed = speed
def __set_year(self, year):
self.__year_model = year
def __set_model(self, make):
self.__make = make
def __set_speed(self, speed):
self.__speed
def __get_year(year):
return self.__year_model
def __get_model(model):
return self.__make
def __get_speed(accelerate):
return self.__speed
#methods
def accelerate(speed):
return (speed + 5)
def brake(speed):
return (speed - 5)
import car
import time
def main():
#Get the model of the car.
year = input('Enter the year of the car: ')
#Get the year of the car.
make = input('Enter the model of the vehicle: ')
speed = 0
print('Okay we are not moving so your current speed is 0')
#Create a car object.
my_car = car.Car(year, make, 0)
for count in range(5):
print('Give it some gas!!!')
time.sleep(2)
my_car.accelerate()
print('Your current speed is: ',my_car.get_speed())
for count in range(5):
print('Whoa whoa whoa slow dowwn!!!')
time.sleep(2)
my_car.brake()
print('Your current speed is:', my_car.get_speed())
print('Your', make, 'runs pretty good.')
main()
Upvotes: 0
Views: 49
Reputation: 7020
There are number of issues with your code. To start with, all class methods should take self
as their first parameters. Secondly, you define some class methods (like accelerate
) that return values but then you aren't using those values. Perhaps in that case you really want to modify the speed within the method. E.g.:
def accelerate(self):
self.__speed += 5
I also notice that you're calling my_car.get_speed()
but you haven't defined any method with that name (just __get_speed
). You probably don't want to be naming your methods with double underscores. Take a look at naming conventions specified in PEP8.
Upvotes: 2
Reputation: 9599
You're missing the self
parameter in your class' functions. It should be:
def accelerate(self, speed):
And remember to pass the speed
argument when calling it!
Upvotes: 2