Class 对象出现意外参数错误
Class object gets unexpected argument error
我是 Python 的新手,正在尝试编码 Python 类。但在下面,对于 car_1、car_2 和 car_3(粗体)的属性,我得到“意外参数”错误。如何纠正?还是 Pycharm 的问题?谢谢!
import random
class Vehicle:
def _init_(self, make, model, year, weight):
self.make = make
self.model = model
self.year = year
self.weight = weight
class Car(Vehicle):
def _init_(self, make, model, year, weight, is_driving=True, trips_since_maintenance=0, needs_maintenance=False):
super().__init__(make, model, year, weight)
self.is_driving = is_driving
self.trips_since_maintenance = 0
self.needs_maintenance = needs_maintenance
def drive(self):
drive = 0
if drive > 0:
self.is_driving = True
def stop(self):
stop = 0
while self.drive():
stop += 1
break
self.is_driving = False
self.trips_since_maintenance += 1
if self.trips_since_maintenance >= 100:
self.needs_maintenance = True
def repair(self):
self.needs_maintenance = False
self.trips_since_maintenance = 0
def randomly_drive_car(car):
drive_times = random.randint(1, 101)
for i in range(drive_times):
Car.drive()
Car.stop()
**car_1 = Car('Honda', 'City', '2018', '1153 kg')
car_2 = Car('Toyota', 'Altis', '2018', '1745 kg')
car_3 = Car('Mazda', '_3', '2020', '1260 kg')**
randomly_drive_car(car_1)
randomly_drive_car(car_2)
randomly_drive_car(car_3)
除了init方法需要加两个下划线外,其他都是正确的。
class Vehicle:
def __init__(self, make, model, year, weight):
...
class Car(Vehicle):
def __init__(self, make, model, year, weight, is_driving=True, trips_since_maintenance=0, needs_maintenance=False):
super().__init__(make, model, year, weight)
编辑:
在第 42/43 行,您正在调用 'Car.drive',您应该调用 'car.drive()'(小写 C),因为您的函数参数被称为 'car'。通过使用 Car.drive()
,您试图将驱动方法作为 class 方法调用。
编辑 2(来自评论):
import random
class Car(Vehicle):
...
def take_trip(self):
self.trips_since_maintenance += 1
if self.trips_since_maintenance >= 100:
self.needs_maintenance = True
def randomly_drive_car(car):
drive_times = random.randint(1, 101)
for i in range(drive_times):
car.take_trip()
car_1 = Car('Honda', 'City', '2018', '1153 kg')
car_2 = Car('Toyota', 'Altis', '2018', '1745 kg')
car_3 = Car('Mazda', '_3', '2020', '1260 kg')
randomly_drive_car(car_1)
randomly_drive_car(car_2)
randomly_drive_car(car_3)
我是 Python 的新手,正在尝试编码 Python 类。但在下面,对于 car_1、car_2 和 car_3(粗体)的属性,我得到“意外参数”错误。如何纠正?还是 Pycharm 的问题?谢谢!
import random
class Vehicle:
def _init_(self, make, model, year, weight):
self.make = make
self.model = model
self.year = year
self.weight = weight
class Car(Vehicle):
def _init_(self, make, model, year, weight, is_driving=True, trips_since_maintenance=0, needs_maintenance=False):
super().__init__(make, model, year, weight)
self.is_driving = is_driving
self.trips_since_maintenance = 0
self.needs_maintenance = needs_maintenance
def drive(self):
drive = 0
if drive > 0:
self.is_driving = True
def stop(self):
stop = 0
while self.drive():
stop += 1
break
self.is_driving = False
self.trips_since_maintenance += 1
if self.trips_since_maintenance >= 100:
self.needs_maintenance = True
def repair(self):
self.needs_maintenance = False
self.trips_since_maintenance = 0
def randomly_drive_car(car):
drive_times = random.randint(1, 101)
for i in range(drive_times):
Car.drive()
Car.stop()
**car_1 = Car('Honda', 'City', '2018', '1153 kg')
car_2 = Car('Toyota', 'Altis', '2018', '1745 kg')
car_3 = Car('Mazda', '_3', '2020', '1260 kg')**
randomly_drive_car(car_1)
randomly_drive_car(car_2)
randomly_drive_car(car_3)
除了init方法需要加两个下划线外,其他都是正确的。
class Vehicle:
def __init__(self, make, model, year, weight):
...
class Car(Vehicle):
def __init__(self, make, model, year, weight, is_driving=True, trips_since_maintenance=0, needs_maintenance=False):
super().__init__(make, model, year, weight)
编辑:
在第 42/43 行,您正在调用 'Car.drive',您应该调用 'car.drive()'(小写 C),因为您的函数参数被称为 'car'。通过使用 Car.drive()
,您试图将驱动方法作为 class 方法调用。
编辑 2(来自评论):
import random
class Car(Vehicle):
...
def take_trip(self):
self.trips_since_maintenance += 1
if self.trips_since_maintenance >= 100:
self.needs_maintenance = True
def randomly_drive_car(car):
drive_times = random.randint(1, 101)
for i in range(drive_times):
car.take_trip()
car_1 = Car('Honda', 'City', '2018', '1153 kg')
car_2 = Car('Toyota', 'Altis', '2018', '1745 kg')
car_3 = Car('Mazda', '_3', '2020', '1260 kg')
randomly_drive_car(car_1)
randomly_drive_car(car_2)
randomly_drive_car(car_3)