【问题标题】:Unable to access parent class' global variable with Child class' object无法使用子类的对象访问父类的全局变量
【发布时间】:2021-09-28 03:04:40
【问题描述】:

我想在有人租车时更新可用汽车的计数器。当我尝试通过子类访问此变量时,它显示错误为“UnboundLocalError: local variable 'available_cars' referenced before assignment”

PS:还在研究中,所以我还没有完成所有的方法代码。

父类

available_cars = 1000

类 Car_rental():

def __init__(self):
    pass

def display_available_cars(self):
    print("Total avaialable cars for renting is:", available_cars)

def rent_hourly(self, cars_rented):
    print("Our hourly rate is $100/hr.")

    if cars_rented > available_cars:
        print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
            available_cars))
    elif cars_rented < available_cars:
        print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
            cars_rented))

    elif cars_rented < 0:
        print ("Please provide a valid number of cars.")

def rent_weekly(self):
    pass

def rent_monthly(self):
    pass

def bill(self):
    pass

def update_invetory(self, cars_rented):
    available_cars = available_cars - cars_rented

儿童班

从 carRental 导入 *

类客户(Car_rental): def 初始化(自我): Car_rental.init(自我)

def rent_cars(self):

    mode = int(input(
        "Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
    if mode == 1 or mode == 'hourly' or mode == 'Hourly':
        cars_rented = int(input("How many cars do you wish to rent?"))
        self.rent_hourly(cars_rented)
        self.update_invetory(cars_rented)

    elif mode == 2 or mode == 'weekly' or mode == 'Weekly':
        self.rent_weekly()
    elif mode == 3 or mode == 'monthly' or mode == 'Monthly':
        self.rent_monthly()
    else:
        print("Please provide appropriate input.")

def return_cars(self):
    pass

【问题讨论】:

    标签: python-3.x object global-variables


    【解决方案1】:

    因此,如果我正确地回答了您的问题,您的代码似乎有目标。

    1. 您想要定义一个 Car_Rental 类,其类级变量 available_cars 最初设置为 1000,然后随着客户租用车辆减少可用汽车的数量。

    2. 您还需要一个继承 Car_Rental 类的第二个 Customer 类,该类实现了计费功能。

    3. 虽然我对此不确定,但您似乎还希望这些类定义中的每一个都驻留在它们自己单独的 python 脚本文件中。

    这就是我将如何解决这些问题。

    # If desired could be placed in separate file Car_Rental.py
    class Car_Rental():
        available_cars = 1000
    
        def display_available_cars(self):
            print("Total avaialable cars for renting is:", Car_Rental.available_cars)
    
        def isOkayToRent(self, nmbr):
            return nmbr <= Car_Rental.available_cars
        
        def rent_hourly(self, cars_rented):
            print("Our hourly rate is $100/hr.")
    
            if cars_rented < 0:
                print ("Please provide a valid number of cars.")
                
            else:
                if self.isOkayToRent(cars_rented):
                    print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
                    cars_rented))
                else:
                    print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
                    Car_Rental.available_cars))
                    
        def rent_weekly(self):
            pass
    
        def rent_monthly(self):
            pass
    
        def bill(self):
            pass
    
        def update_inventory(self, nmbr):                       
            assert nmbr > 0
            Car_Rental.available_cars -= nmbr
            
        @property
        def cars(self):
            return Car_Rental.available_cars
    
    # Can be placed in  second file. 
    
    #Note include this next line if classes are stored in separate files 
    #from Car_rental import Car_Rental 
    
    
    class Customer(Car_Rental): 
        def __init__(self):
            Car_Rental.__init__(self)
    
        def rent_cars(self):
    
            mode = int(input(
                "Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
            if mode == 1:
                cars_rented = int(input("How many cars do you wish to rent?"))
                self.rent_hourly(cars_rented)
                self.update_inventory(cars_rented)
    
            elif mode == 2:
                self.rent_weekly()
            elif mode == 3:
                self.rent_monthly()
            else:
                print("Please provide appropriate input.")
    
        def return_cars(self):
            pass  
    

    使用方法:

    c1 = Customer()
    c1.cars  
    

    1000

    c1.rent_cars()  
    Please select the mode of renting the car:
    1. Hourly
    2. Weekly
    3. Monthly
    How many cars do you wish to rent? 2
    Our hourly rate is $100/hr.
    Thank you for renting 2 cars from Swift car renting portal. Enjoy you ride.  
    

    为了说明适当的库存减少: c1.cars 产生 998

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2019-12-04
      • 1970-01-01
      • 2016-12-27
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多