Mar-30-2020, 02:47 PM
(This post was last modified: Mar-30-2020, 02:55 PM by horuscope42.)
class Car: """A simple attempt to represent a car.""" def __int__(self, make, model, year): """Initialize the car.""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """Return a description of the car in a neat format.""" long_name = f"{self.year} {self.make} {self.model}" return long_name.title() def read_odometer(self): """Creates a statement of the car mileage.""" print(f"This car has {self.odometer_reading} miles on it.") def update_odometer(self, mileage): """Create a method for updating the mileage of the odometer.""" if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print(f"You can't roll back an odometer.") def increment_odometer(self, miles): """Defines the increment increase of the odometer.""" self.odometer_reading += miles """The '+=' symbology has been confusing but I tend to think of it as, ex) 'a += b'' is == to 'a = a + b'. So in this example, on line 29, the semantic readthrough could be interpreted as the 'self.odometer_reading = self.odometer_reading + miles'.""" #When creating a child class, the parent class must be part of the current file and must appear before the child class in the file.# #Next we will define the child class, 'ElectricCar.# class ElectricCar(Car): #We are creating a child class called 'ElectricCar' from the parent class 'Car'# """Represents aspects of a car, specific to electric vehicles.""" def __int__(self, make, model, year): """Initialize attributes of the parent class.""" super().__int__(make, model, year) my_tesla = ElectricCar('tesla', 'model s', 2019) print(my_tesla.get_descriptive_name())
Error:Traceback (most recent call last):
File "C:\Users\Carlo\Documents\Python Material\python_work\parent_child_classes.py", line 45, in <module>
my_tesla = ElectricCar('tesla', 'model s', 2019)
TypeError: ElectricCar() takes no arguments
I apologize about the previous post, I had some comments in there that I thought weren't relevant and took them out without considering the line changes.Thanks for the help people; Larz60+ for help posting questions properly and to buran for spotting that silly mistake.