Python名称错误,将类变量传递给函数

时间:2016-06-03 07:33:07

标签: function class python-3.x nameerror

我是班级数据类型的新手。我将以下类作为我的程序的一部分。我正在尝试将类变量调用到函数中:

        class bicycle(object):
       #class containing model name of bicycle,weight and cost to produce 
          def __init__(self,model_name,weight,cop):
            self.model_name = model_name
            self.weight = weight
            self.cop = int(cop + cop * .20)

          def print_list(self):
            print(self.model_name,end=" ")
            print(self.weight,end=" ")
            print(self.cop)


        #class bike_shops(self,shop_name,inventory,sel_price)
         class shop(object):
          def __init__(self,model_name,count):
            self.model_name = model_name
            self.count = count

        #class customers(self,cus_name,buget)
        class customers(object):
          def __init__(self,cus_name,buget):
           self.cus_name = cus_name
        self.buget = buget

        def purchase(name,input_code):
          if input_code == "001":
            temp = model_a.cop
            print("Bike purchased is Model A")
            print("Cost of bike is %s " %(temp))
            print("Money left in bicycle fund is %s" %(customer_a.buget - model_a.cop))
            model_a_in.count = model_a_in.count - 1

    def main():
        model_a = bicycle("Model A","10 KG",int(120))
        model_b = bicycle("Model B","8 KG",int(220))
        model_c = bicycle("Model C","7 KG",int(500))
        model_d = bicycle("Model D","7 KG",int(550))
        model_e = bicycle("Model E","6 KG",int(600))
        model_f = bicycle("Model F","4 KG",int(800))
        customer_a = customers("Anshul",int(200))
        customer_b = customers("Ram",int(500))
        customer_c = customers("Sham",int(1000))
        model_a_in = shop("Model A",int(5))
        model_b_in = shop("Model B",int(7))
        model_c_in = shop("Model C",int(9))
        model_d_in = shop("Model D",int(7))
        model_e_in = shop("Model E",int(3))
        model_f_in = shop("Model F",int(2))

        list_bikes = { model_a.cop:model_a.model_name,model_b.cop:model_b.model_name,model_c.cop:model_c.model_name,model_d.cop:model_d.model_name,model_e.cop:model_e.model_name,model_f.cop:model_f.model_name }

print("%s can afford " %(customer_a.cus_name))
for key in list_bikes:
  if customer_a.buget >= key:
    print(list_bikes[key])


  if customer_b.buget >= key:
    print(list_bikes[key])

print("%s can afford " %(customer_c.cus_name))
for key in list_bikes:
  if customer_c.buget >= key:
    print(list_bikes[key])
list_bikes = [model_a.cop,model_b.cop,model_c.cop,model_d.cop,model_e.cop,model_f.cop]

print("Printing inventorty of the shop")
print("%s : Inventory: %s  Product code:001 " %(model_a_in.model_name,model_a_in.count))
print("%s : Inventory: %s  Product code:002" %(model_b_in.model_name,model_b_in.count))
print("%s : Inventory: %s  Product code:003" %(model_c_in.model_name,model_c_in.count))
print("%s : Inventory: %s  Product code:004" %(model_d_in.model_name,model_d_in.count))
print("%s : Inventory: %s  Product code:005" %(model_e_in.model_name,model_f_in.count))
print("%s : Inventory: %s  Product code:006" %(model_f_in.model_name,model_f_in.count))
print("\n")

print("Welcome Anshul")
print("Enter code of bicycle to purchase")
input_code = input()
code_list = ["001","002","003","004","005","006"]
if input_code in code_list:
    purchase("Anshul",input_code)
else:
    print("Invalid code\n")

运行程序时出现以下错误。我可以将类变量传递给函数吗?

Traceback (most recent call last):
  File "bicycle.py", line 131, in <module>
    main()
  File "bicycle.py", line 112, in main
    purchase("Anshul",input_code)
  File "bicycle.py", line 28, in purchase
    temp = model_a.cop 
NameError: name 'model_a' is not defined

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您遵循追溯,您可以看到未在“购买”功能中定义model_a。

要解决这个问题,我会改变你的:

def purchase(name, input_code):

def purchase(name, input_code, model):

这样,当您调用“购买”功能时。将模型作为参数发送,您可以从函数中调用自定义方法。