I am trying to write a program that contains a list of cars and SUVs. The user should be asked which car model (f.ex Tesla) he/she would like to have printed out. All items of that model should then be printed. The code below gives me error message TypeError: 'Car' object is not subscriptable - any ideas?
class Vehicle:
def __init__(self, make, model, year, mileage, price):
self.make = make
self.model = model
self.year = year
self.mileage = mileage
self.price = price
def getValues(self):
return self.make " " self.model " " self.year " " self.mileage " " self.price
def Display(self):
print("Invetory Unit: Car \n Make: " self.make "\n Model: " self.model "\n Year " self.year "\n Miles " self.mileage " \n Price :" self.price)
class Car(Vehicle):
def __init__(self, make, model, year, mileage, price, num_doors):
Vehicle.__init__(self,make, model,year,mileage,price)
self.num_doors = num_doors
def GetCar(self):
return self.getValues() ", " self.num_doors
def Display(self):
print("Invetory Unit: Car \n Make: " self.make "\n Model: " self.model "\n Year " self.year "\n Miles " self.mileage " \n Price :" self.price " \n Number of doors :" self.num_doors)
class SUV(Vehicle):
def __init__(self, make, model, year, mileage, price, passcap):
Vehicle.__init__(self, make, model, year, mileage, price)
self.passcap = passcap
def GetSUV(self):
return self.getValus() ", " self.passcap
def Display(self):
print("Invetory Unit: SUV \n Make: " self.make "\n Model: " self.model "\n Year " self.year "\n Miles " self.mileage " \n Price :" self.price " \n Passenger capacity :" self.passcap)
def main():
vehicles_list = []
vehicles_list.append(Car("Tesla", "S", "2020", "170000", "33000.0", "4"))
vehicles_list.append(SUV("Tesla", "x", "2021", "180000", "23000.0", "6"))
vehicles_list.append(Car("Tesla", "3", "2021", "180000", "23000.0", "4"))
vehicles_list.append(Car("Ford", "Mustang", "2019", "140000", "22000.0", "2"))
vehicles_list.append(SUV("Ford", "Explorer", "2021", "112000", "12000.0", "2"))
selectedModel = input('Which car model would you like to print?: ')
print([vehicle for vehicle in vehicles_list if vehicle[0] == model])
selectedModel = input('Which car model would you like to print?: ')
for vehicle in (vehicle for vehicle in vehicles_list if vehicle[0] == selectedModel):
vehicle.Display()
main()
CodePudding user response:
Replace vehicle[0] with vehicle.model and also model in your first query is undefined so you probably should put selectedModel.
CodePudding user response:
A few things I have fixed here:
Empty list should be initialized with
list()not[]selected_modelnotselectedModelfor snake casevehicle.makenotvehicle[0]
just FYI you can change Display method to __repr__ or __str__ in the other classes - it will automatically then render when you do something like print(vehicle)
def main():
vehicles_list = list()
vehicles_list.append(Car("Tesla", "S", "2020", "170000", "33000.0", "4"))
vehicles_list.append(SUV("Tesla", "x", "2021", "180000", "23000.0", "6"))
vehicles_list.append(Car("Tesla", "3", "2021", "180000", "23000.0", "4"))
vehicles_list.append(Car("Ford", "Mustang", "2019", "140000", "22000.0", "2"))
vehicles_list.append(SUV("Ford", "Explorer", "2021", "112000", "12000.0", "2"))
selected_model = input('Which car model would you like to print?: ')
print([vehicle for vehicle in vehicles_list if vehicle.make == selected_model])
selected_model = input('Which car model would you like to print?: ')
for vehicle in (vehicle for vehicle in vehicles_list if vehicle.make == selected_model):
vehicle.Display()
