lundi 11 janvier 2016

what am I doing wrong with if in python?

I'm new to python, and maybe I ask a silly question, but I don't know why my code doesn't work... I have a delete method into a repository file which iterates through an array of cars, and if it finds a car that has the same id, deletes it.

This is my code:

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())

and this is my output:

1.Cars menu
2.Clients menu
3.Rentals menu
0.Exit

Command: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

Command: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)

Enter the id of the car you want to delete: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

I don't understand why does if continues to iterate after checking 2=2... it doesn't seem to be equal for python...

Any help?

Aucun commentaire:

Enregistrer un commentaire