我是python的新手,也许我问一个愚蠢的问题,但我不知道为什么我的代码不起作用...... 我有一个删除方法到一个存储库文件,它遍历一系列汽车,如果它找到一个具有相同ID的汽车,删除它。
这是我的代码:
def delete_car(self, carId):
noOfCars = len(self.carsArray)
for i in range(0, noOfCars):
print("Entered for")
if (self.carsArray[i].get_id() == carId):
print("found the id!")
print(carId, self.carsArray[i].get_id())
del self.carsArray[i]
break
else:
print("didn't find any matching id", carId, self.carsArray[i].get_id())
这是我的输出:
1.Cars menu
2.Clients menu
3.Rentals menu
0.Exit
命令:1
1.Show available cars
2.Add a car
3.Delete a car
4.Update car
5.Show cars by their manufacturer
6.Find a car
0.Exit
命令:3
1 Mustang Ford(A very nice muscle car)
2 Prius Toyota(A hybrid car)
3 Passat Volkswagen("The car of the people")
4 Logan Dacia(A Romanian made car)
输入您要删除的汽车的ID:2
Entered for
didn't find any matching id 2 1
Entered for
didn't find any matching id 2 2
Entered for
didn't find any matching id 2 3
Entered for
didn't find any matching id 2 4
我不明白为什么在检查2 = 2之后继续迭代...它对于python来说似乎不相等......
任何帮助?