2016-06-03 3 views
0

클래스 데이터 유형에 익숙하지 않습니다. 내가 프로그램을 실행할 때클래스 변수를 함수에 전달하는 Python 이름 오류

 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

trackeback에 명확한 정보가 있습니다 :'model_a'는'purchase (name, input_code)'메소드 안에 정의되어 있지 않습니다. –

답변

0

추적을 따르면 model_a가 "구매"기능에 정의되어 있지 않음을 알 수 있습니다.

이 문제를 해결하려면, 당신을 바꿀 것 :

def purchase(name, input_code): 

이런 식으로

def purchase(name, input_code, model): 

에, 당신은 "구매"함수를 호출 할 때. 모델을 매개 변수로 보내면 함수 내에서 사용자 정의 메서드를 호출 할 수 있습니다.