mitcov
mitcov

Reputation: 65

How to fix my IF statement so I cant remove and append items from a list

I'm currently writing up a simple program to manage a car yard for a rental company, i've gotten to the end with no real issues until now. To illustrate what I mean i'll posted my code and then my issue.

# class for Car_Yard
class CarYard():

    def __init__(self, listOfCars):
        self.availableCars = listOfCars

    def carYardCarsAvailable(self):
        print("Available Cars: ")
        for car in self.availableCars:
            print(car)

    def carYardRentCar(self, rentCar):
        if rentCar in self.availableCars:
            print("Congratulations on renting your new car, here\'s the keys")
            self.availableCars.remove(rentCar)
        else:
            print("We currently don't have that car in our yard")

    def carYardReturnCar(self, rentCarReturn):
        self.availableCars.append(rentCarReturn)
        print("You have returned the car. Thank You!")


# class for Buyer and his/hers actions
class Buyer():

    def buyerRentCar(self):
        print("Which car would you like to rent out?" )
        self.car = input()
        return self.car

    def buyerReturnCar(self):
        print("Which car would you like to return? ")
        self.car = input()
        return self.car


# create objects from class and pass a list of cars to the car yard
carYard = CarYard (['Mazda','Holden','Ford','Porsche','Honda','VW','Toyota','Kia'])
buyer = Buyer

# infinite loop
while True:
    print()
    print("Enter 1 to see our wide range of cars")
    print("Enter 2 to rent a car")
    print("Enter 3 to return a car")
    print("Enter 4 to leave the rental yard")
    print()

    userInput = int(input())

    if userInput is 1:
        carYard.carYardCarsAvailable()
    elif userInput is 2:
        rentCar = buyer.buyerReturnCar
        carYard.carYardRentCar(rentCar)
    elif userInput is 3:
        rentCarReturn = buyer.buyerReturnCar
        carYard.carYardReturnCar(rentCarReturn)
    elif userInput is 4:
        quit()

The issue that i'm having is when I run my code and enter 2 it automatically skips to the line "we currently don't have that car in our yard" and when I enter 3 is says "You have returned the car. Thank you!".

I'm trying to figure out why my code is not called the Buyer class to request for the input. Any suggestions on what I might be missing?

Upvotes: 0

Views: 332

Answers (1)

Michael
Michael

Reputation: 2414

You shouldn't use is like that. The is operator tests whether 2 objects are the same, which is not the same as testing whether their values are equal. What you actually want is an equality test (e.g. userInput == 1).

Anyway, the source of your problem is that you're passing around methods instead of the values that are returned by those methods. For instance this may work better:

buyer = Buyer()
...
elif userInput == 2:
    rentCar = buyer.buyerReturnCar()
    carYard.carYardRentCar(rentCar)

By passing buyer.buyerRentCar you're passing a method to carYardRentCar, and naturally it's not able to match that method against anything in the list of cars. What you want is to pass a string that is returned by carYardRentCar(). This will cause that method to get invoked, asking the user for input, and then the result of that will get passed along, which is what you want

Upvotes: 1

Related Questions