这是我的代码:
while True:
print(vehiclelist)
reg = input('Enter registration number of vehicle: ')
if reg in vehiclelist:
break
else:
print("Invalid")
但它一直显示无效,这是输出:
[Car('SJV1883R','Honda','Civic',60.00),Car('SJZ2987A','Toyota','Altis',60.00),Car('SKA4370H','Honda','Accord ',80.00), 汽车('SKD8024M','丰田','凯美瑞',80.00),汽车('SKH5922D','宝马', '320i',90.00),车('SKM5139C','宝马','520i',100.00), 汽车('SKP8899H','梅赛德斯','S500',300.00),卡车('GB3221K','塔塔', '魔术',200.00),卡车('YB8283M','五十铃','NPR',250.00), 卡车('YK5133H','五十铃','NQR',300.00)]
输入车辆登记号码:SJZ2987A
无效
知道如何检查输入吗?
这是我的车辆类:
class Vehicle():
def __init__(self, regNo, make, model, dailyRate, available):
self.regNo = regNo
self.make = make
self.model = model
self.dailyRate = dailyRate
self.available = available
@property
def dailyRate(self):
return self.__dailyRate
@dailyRate.setter
def dailyRate(self, dailyRate):
if dailyRate < 0:
self.__dailyRate = 0
else:
self.__dailyRate = dailyRate
def __repr__(self):
return "Vehicle('{:s}', '{:s}', '{:s}', {:.2f}, '{:s}')".format(self.regNo, self.make, self.model, self.dailyRate, self.available)
答案 0 :(得分:1)
这里的问题是vehicle_list
是一个Vehicle对象列表,你不能直接在车辆对象列表中搜索注册号。
更好的设计模式是使用字典,其中regNo
将显示为关键字,车辆对象将显示为值。
您可以按如下方式更改代码:
vehicle_details = {vehicle.regNo : vehicle for vehicle in vehiclelist}
while True:
reg = input('Enter registration number of vehicle: ')
if reg in vehicle_details:
break
else:
print("Invalid")
答案 1 :(得分:0)
您需要迭代各个汽车对象并比较regNo
def getInput():
while True:
print(vehiclelist)
reg = input('Enter registration number of vehicle: ')
for car in vehiclelist:
if reg == car.regNo:
return
print("Invalid")