class Car:
def __init__(self, brand, color, model, year, mileage):
self.brand = brand
self.color = color
self.year = year
self.model = model
self.mileage = mileage
def __str__(self):
return f'TYPE: NORMAL CAR\nBrand: {self.brand}\nColor: {self.color}\n'
def __add__(self, other):
line = '- ' * 15
return '\n'.join([str(self), self.documentation(), line, str(other), other.documentation(), line])
def documentation(self):
return f'DOCUMENTATION\nModel: {self.model}\nYear: {self.year}\nMileage: {self.mileage} Km'
class SportCar(Car):
def __init__(self, brand, color, model, year, mileage, engine, maxspeed):
super().__init__(brand, color, model, year, mileage)
self.engine = engine
self.maxspeed = maxspeed
def __str__(self):
return f'TYPE: SPORT CAR\nBrand: {self.brand}\nColor: {self.color}\n'
def documentation(self):
return f'DOCUMENTATION\nModel: {self.model}\nYear: {self.year}\nMileage: {self.mileage} Km\n' \
f'Engine: {self.engine}\nMax Speed: {self.maxspeed}'
car1 = SportCar('Audi', 'Black', 'R8', '2018', '35 000', 'V6', '350 Km/h')
car2 = Car('Skoda', 'White', 'Superb', '2015', '165 000')
car3 = Car('Honda', 'Blue', 'Civic', '2017', '220 000')
car4 = SportCar('BMW', 'Red', 'M5', '2020', '10 000', 'V8', '430 Km/h')
print(car1 + car2 + car3 + car4)
I am trying to sum up all 4 instances to show their details but it doesn't work I know if I print(car1 + car2) and under it print(car3 + car4) will result in the thing I want but I was just curious how to make the code to be able to run it like above, all 4 summed up and eventually everything I keep adding to work fine Also print(str(car1 + car2) + '\n' + str(car3 + car4)) works too but still.. Again, I am quiet new to python and it's also my first language, thanks :v