简体   繁体   English

UnboundLocalError:分配前已引用局部变量“ cars”

[英]UnboundLocalError: local variable 'cars' referenced before assignment

I am making a program where I store and display an inventory of cars through a menu. 我正在编写一个程序,用于通过菜单存储和显示汽车清单。 However, I keep on receiving the error: 但是,我继续收到该错误:

".py", line 24, in car_invent
    cars = cars.Car(make, model, mileage, price, doors)
UnboundLocalError: local variable 'cars' referenced before assignment"

I've already imported the module in the beginning and I don't understand why it gives me this error 我已经在开始时导入了模块,但我不明白为什么它会给我这个错误

My code is 我的代码是

import cars

def print_desc(item):
    print "Make:", item.get_make()
    print "Model:", item.get_model()
    print "Mileage:", item.get_mileage()
    print "Price:", item.get_price()

def car_invent():
    car_invent = []

    print "Enter data for the cars."

    num_cars = input("Enter number of cars: ")

    for count in range(num_cars):
        make = raw_input("Enter the make: ")
        model = input("Enter the year model: ")
        mileage = input("Enter the mileage: ")
        price = input("Enter the price: ")
        doors = input("Enter the number of doors: ")

        cars = cars.Car(make, model, mileage, price, doors)

        car_invent.append(cars)

    return car_invent

def truck_invent():
    truck_invent = []

    print "Enter data for the trucks."

    num_trucks = input("Enter number of trucks: ")

    for count in range(num_trucks):
        make = raw_input("Enter the make: ")
        model = input("Enter the year model: ")
        mileage = input("Enter the mileage: ")
        price = input("Enter the price: ")
        drive_type = input("Enter the drive type: ")

        trucks = cars.Truck(make, model, mileage, price, drive_type)

        truck_invent.append(truck)

    return truck_invent

def suv_invent():
    suv_invent = []

    print "Enter data for the cars."

    num_suv = input("Enter number of SUVs: ")

    for count in range(1, num_suv):
        make = raw_input("Enter the make: ")
        model = input("Enter the year model: ")
        mileage = input("Enter the mileage: ")
        price = input("Enter the price: ")
        pass_cap = input("Enter passenger capacity: ")

        suv = cars.SUV(make, model, mileage, price, pass_cap)

        suv_invent.append(suv)

    return suv_invent


def read_invent(car_invent, truck_invent, suv_invent):
    print "USED CAR INVENTORY"
    print "=================="
    print "The following car is in inventory."

    for item in car_invent:
        print_desc(item)
        print "Number of doors:", item.get_doors()

    print "The following pickup truck is in inventory."

    for item in truck_invent:
        print_desc(item)
        print "Drive type:", item.get_drive_type()


    print "The following SUV is in inventory."

    for item in suv_invent:
        print_desc(item)
        print "Passenger Capacity:", item.get_pass_cap()

def menu():
    print "MENU"
    print "====="
    print "1. Enter data for inventory"
    print "2. Display inventory"
    print "3. Quit"

def main():
    menu()

    choice = input("Enter choice: ")
    while choice != 3:

        if choice == 1:
            cars = car_invent()
            trucks = truck_invent()
            suv = suv_invent()
            choice = input("Enter choice: ")

        elif choice == 2:
            read_invent(cars, trucks, suv)
            choice = input("Enter choice: ")

        else:
            print "Invalid choice"
            choice = input("Enter choice: ")

main()

Here is also the module for reference: 这也是供参考的模块:

class Automobile:

    def __init__(self, make, model, mileage, price):
        self.__make = make
        self.__model = model
        self.__mileage = mileage
        self.__price = price

    def set_make(self, make):
        self.__make = make

    def set_model(self, model):
        self.__model = model

    def set_mileage(self, mileage):
        self.__mileage = mileage

    def set_price(self, price):
        self.__price = price

    def get_make(self):
        return self.__make
    def get_model(self):
        return self.__model

    def get_mileage(self):
        return self.__mileage
    def get_price(self):
        return self.__price

class Car(Automobile):
    def __init__(self, make, model, mileage, price, doors):
        Automobile.__init__(self, make, model, mileage, price)

        self.__doors = doors

    def set_doors(self, doors):
        self.__doors = doors

    def get_doors(self):
        return self.__doors

class Truck(Automobile):
    def __init__(self, make, model, mileage, price, drive_type):
        Automobile.__init__(self, make, model, mileage, price)

        self.__drive_type = drive_type

    def set_drive_type(self, drive_type):
        self.__drive_type = drive_type

    def get_drive_type(self):
        return self.__drive_type

class SUV(Automobile):
    def __init__(self, make, model, mileage, price, pass_cap):
        Automobile.__init__(self, make, model, mileage, price)

        self.__pass_cap = pass_cap

    def set_pass_cap(self, pass_cap):
        self.__pass_cap = pass_cap

    def get_pass_cap(self):
        return self.__pass_cap
cars = cars.Car(make, model, mileage, price, doors)
  1. You declare a local variable named cars 您声明一个名为cars的局部变量

  2. You assign cars.Car(...) to cars 您分配cars.Car(...)cars

  3. But cars is a local variable which doesn't have a value yet (unbound local) 但是cars是一个尚无值的局部变量(未绑定的局部变量)

  4. Your program crashs 您的程序崩溃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:分配前已引用局部变量“ ticketCost” - UnboundLocalError: local variable 'ticketCost' referenced before assignment UnboundLocalError:赋值前引用了局部变量“df” - UnboundLocalError: local variable 'df' referenced before assignment UnboundLocalError:分配前已引用局部变量“ getBottles” - UnboundLocalError: local variable 'getBottles' referenced before assignment UnboundLocalError:分配前已引用局部变量“(Var)” - UnboundLocalError: local variable '(Var)' referenced before assignment 'UnboundLocalError:赋值前引用了局部变量'lower'' - 'UnboundLocalError: local variable 'lower' referenced before assignment' UnboundLocalError(分配前引用的局部变量) - UnboundLocalError (local variable referenced before assignment) UnboundLocalError:分配前引用了本地变量“ url” - UnboundLocalError: local variable 'url' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ p” - UnboundLocalError: local variable 'p' referenced before assignment UnboundLocalError:分配前已引用本地变量“电子邮件” - UnboundLocalError: local variable 'email' referenced before assignment UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM