1

问题是

编写一个名为 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()
4

4 回答 4

3

__init__for类需要三个参数,Car你没有传递任何参数。

year=input('car year: '))
make=input('car make: '))
speed=0
print'Current speed is', speed
#actuall program
#create car object
def main():
my_car=Car(year, make, speed)

您不需要printinput. 我不确定你要做什么。也不speed= print('Current speed is 0')正确。

此函数始终将速度设置为零,如果这是您想要的,则无需传递参数speed。与您的 init 相同,如果您不使用它,则无需传递参数。

def set_speed(self,speed):
    self.__speed=0
于 2013-11-05T01:31:24.480 回答
1

错误消息会告诉您究竟出了什么问题。您已定义Car需要三个参数:

def __init__(self, year, make, speed):

但是当您创建一个时,您不会传递任何这些:

my_car = Car()

看?括号里什么都没有。显然,这需要:

my_car = Car(year, make, speed)

当然,现在你不能这样做,因为你还没有要求这些项目。因此,在从用户那里获取该信息my_car 后创建。

于 2013-11-05T02:05:44.880 回答
0

class Car(object): # made class called car def init (self, model, make): self.__model = model self.__make = make self.__speed = 0 def acceleration(self): self.__speed += 5 #increase 5 速 def 刹车(self): self.__speed -= 5 #decrease 5 speed

def get_speed(self):
return self.__speed #return speed

wr_handle = open("output_04.txt", "w+")
carObject = Car( 1984,"Chevron")
wr_handle.write("Car is accelerating: \n")
print("Car is accelerating:") #accelerate 5 times
for p in range(0, 5):
carObject.accelerate()
my_car_speed = carObject.get_speed()
wr_handle.write("The current speed is: %d \n"% my_car_speed)
print("The current speed is: %d "% my_car_speed)

wr_handle.write("Car is braking: \n")
print("Car is braking: ") #brake 5 times
for p in range(0,5):
carObject.brake()
my_car_speed = carObject.get_speed()
wr_handle.write("The current speed is: %d \n"% my_car_speed)
print("The current speed is: %d "% my_car_speed)

wr_handle.write("Bye! \n")
print("Bye! ")
wr_handle.close()
于 2019-07-17T17:41:57.523 回答
-1

class Car(object): # made class called car def init (self, model, make): self.__model = model self.__make = make self.__speed = 0 def acceleration(self): self.__speed += 5 #increase 5速

def brake(self):
    self.__speed -= 5 #decrease 5 speed
于 2019-07-17T17:54:24.387 回答