我目前正在写一个简单的程序来管理一个租车公司的停车场,我已经到了最后没有真正的问题,直到现在。为了说明我的意思,我将发布我的代码,然后发布我的问题。在
# 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()
我遇到的问题是,当我运行代码并输入2时,它会自动跳转到“我们目前没有那辆车在我们的院子里”,当我输入3时,是说“你已经还车了”。谢谢你!“。在
我试图弄清楚为什么我的代码没有被调用为Buyer类来请求输入。对我可能遗漏的东西有什么建议吗?在
你不应该那样使用
is
。is
运算符测试两个对象是否相同,这与测试它们的值是否相等不同。你真正想要的是一个等式测试(例如userInput == 1
)。在不管怎样,问题的根源是,你传递的是方法,而不是那些方法返回的值。例如,这可能会更好:
通过传递
buyer.buyerRentCar
你就是在向carYardRentCar
传递一个方法,自然它无法将该方法与汽车列表中的任何内容相匹配。您需要传递一个由carYardRentCar()
返回的字符串。这将导致调用该方法,请求用户输入,然后结果将被传递,这正是您想要的相关问题 更多 >
编程相关推荐