0

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()
Amarillo
  • 59
  • 6

2 Answers2

0

Replace vehicle[0] with vehicle.model and also model in your first query is undefined so you probably should put selectedModel.

toni057
  • 582
  • 1
  • 4
  • 10
0

A few things I have fixed here:

  1. Empty list should be initialized with list() not []

  2. selected_model not selectedModel for snake case

  3. vehicle.make not vehicle[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()
Helios
  • 675
  • 4
  • 13
  • Thanks a lot for the help! I am probably doing somethin wrong her, but after implementeing your code and typing Tesla i get the following [<__main__.Car object at 0x000001A8D597FFA0>, <__main__.SUV object at 0x000001A8D597FD90>, <__main__.Car object at 0x000001A8D597FCD0>] - any ideas what has happened? – Amarillo Jan 30 '22 at 19:27
  • yes - as I said in my answer, print for a custom object out of the box does not look very nice. Try renaming `def Display(self):` as `def __repr__(self):`, this is the function `print` will look for in this case https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr – Helios Jan 30 '22 at 19:36
  • You would also need to return a string from the function, not print it out https://www.educative.io/edpresso/what-is-the-repr-method-in-python – Helios Jan 30 '22 at 19:59