问题是
编写一个名为 Car 的类,该类具有以下数据属性:
__year_model
(对于汽车的年份模型)
__make
(对于汽车的制造商)
__speed
(对于汽车的当前速度)
Car 类应该有一个__init__
接受汽车年份模型并作为参数的方法。它还应该将 0 分配给__speed
数据属性。
该类还应具有以下方法:
• Accelerate 每次调用时,Accelerate 方法应将速度数据属性加5。
• 制动 每次调用制动方法时,都应从速度数据属性中减去 5。
• get_speed get_speed 方法应该返回当前速度
接下来,设计一个程序,创建一个汽车对象,然后调用加速方法五次。每次调用加速方法后,获取汽车的当前速度并显示出来。然后调用五次刹车方法。每次调用刹车方法后,获取汽车的当前速度并显示出来。
我收到错误TypeError: __init__() missing 3 required positional arguments: 'year', 'make', and 'speed'
我的代码:
#create class called car
class Car:
def __init__(self,year,make,speed):
self.__year_model=year
self.__make=make
self.__speed=0
#set the arguments for speed,year, and make
def set_year_model(self,year):
self.__year_model=year
def set_make(self,make):
self.__make=make
def set_speed(self,speed):
self.__speed=0
#the returns for speed, year, and make
def get_year_model(self):
return self.__year_model
def get_make(self):
return self.__make
def get_speed(self):
return self.__speed
#methods
def accelerate(self):
self.speed +=5
def brake(self):
self.speed-=5
def get_speed(self):
return self.speed
#actuall program
#create car object
def main():
my_car=Car()
year=print(input('car year: '))
make=print(input('car make: '))
speed= print('Current speed is 0')
#accelerate 5 times
my_car.accelerate()
print('My current speed:',my_car.get_speed())
my_car.accelerate()
print('My current speed:',my_car.get_speed())
my_car.accelerate()
print('My current speed:',my_car.get_speed())
my_car.accelerate()
print('My current speed:',my_car.get_speed())
my_car.accelerate()
print('My current speed:',my_car.get_speed())
#brake five times
my_car.brake()
print('My current speed after brake:',my_car.get_speed())
my_car.brake()
print('My current speed after brake:',my_car.get_speed())
my_car.brake()
print('My current speed after brake:',my_car.get_speed())
my_car.brake()
print('My current speed after brake:',my_car.get_speed())
my_car.brake()
print('My current speed after brake:',my_car.get_speed())
main()