2017-02-24 6 views
0

私は、カープログラムを作成してPythonの継承に取り組んでいますが、ビルドに問題が発生しました。Pythonカープログラムの継承

Traceback (most recent call last): 
    File "electric_car.py", line 39, in <module> 
    my_tesla = ElectricCar('tesla', 'model s', 2016) 
    File "electric_car.py", line 37, in __init__ 
    super().__init__(make, model, year) 
TypeError: super() takes at least 1 argument (0 given) 

任意のアイデア:以下は私のコードです:プログラムを実行しようとしたとき

class Car(): 
    """A simple attempt to represent a car""" 
    def __init__(self, make, model, year): 
     """Initialize attributes to describe a car.""" 
     self.make = make 
     self.model = model 
     self.year = year 
     #setting a default value for an attribute# 
     self.odometer_reading = 0 

    def get_descriptive_name(self): 
     """Return a neatly formatted descriptive name.""" 
     long_name = str(self.year) + ' ' + self.make + ' ' + self.model 
     return long_name.title() 

    def read_odometer(self): 
     """Print a statement showing the car's mileage.""" 
     print("This car has " +str(self.odometer_reading) + " miles on it.") 

    def update_odometer(self, mileage): 
     """Modifying the value through the following method 
      Reject the change if it attempts to roll the odometer back 
     """ 
     if mileage >= self.odometer_reading: 
      self.odometer_reading = mileage 
     else: 
      print("You can't roll back an odometer!") 

    def increment_odometer(self, miles): 
     """Incremeting an attributes value through methods""" 
     self.odometer_reading += miles 

class ElectricCar(Car): 
    """Represent aspects of a car, specific to electric vehicles.""" 
    def __init__(self, make, model, year): 
     """Initialize attributes of the parent class.""" 
     super().__init__(make, model, year) 

my_tesla = ElectricCar('tesla', 'model s', 2016) 
print(my_tesla.get_descriptive_name()) 

しかし、私はこのエラーメッセージを取得していますか? python 2.7で

+2

エラーがかなり明確ようだ:*:あなたはおそらく、Pythonの2.xのを使用している*「例外TypeErrorスーパー()は、少なくとも1引数が(0が与えられた)かかり」 –

+0

ああ、それは私がPython 3にアップグレードする必要があるようです。ありがとう! –

+0

また、 'Car'を' object'から拡張したいと思うでしょう –

答えて

0

、私はあなたの

super().__init__(make, model, year) 

Car.__init__(self, make, model, year) 

にすべてが動作しているように変更。出力は次のとおりです。

2016 Tesla Model S