打印列表中的某些元素 (Python)
Print certain elements from a list (Python)
我正在尝试编写一个包含汽车和 SUV 列表的程序。应该询问用户想要打印哪种车型(f.ex 特斯拉)he/she。然后应打印该模型的所有项目。下面的代码给我错误消息 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()
将 vehicle[0]
替换为 vehicle.model
并且第一个查询中的 model
未定义,因此您可能应该输入 selectedModel
.
我在这里修复了一些问题:
应使用 list()
而非 []
初始化空列表
selected_model
不是 selectedModel
蛇形盒
vehicle.make
不是 vehicle[0]
仅供参考,您可以将 Display
方法更改为 __repr__
或 __str__
另一个 类 - 当您执行 [=20] 之类的操作时,它会自动呈现=]
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()
我正在尝试编写一个包含汽车和 SUV 列表的程序。应该询问用户想要打印哪种车型(f.ex 特斯拉)he/she。然后应打印该模型的所有项目。下面的代码给我错误消息 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()
将 vehicle[0]
替换为 vehicle.model
并且第一个查询中的 model
未定义,因此您可能应该输入 selectedModel
.
我在这里修复了一些问题:
应使用
初始化空列表list()
而非[]
selected_model
不是selectedModel
蛇形盒vehicle.make
不是vehicle[0]
仅供参考,您可以将 Display
方法更改为 __repr__
或 __str__
另一个 类 - 当您执行 [=20] 之类的操作时,它会自动呈现=]
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()