2015-02-07 20 views
2

我創建的這裏面有兩個函數的類的功能。這些函數永遠在代碼底部的循環中運行。但是,第一個函數會創建一個字典,並且用戶將值添加到此字典中。第二個函數是爲了導入字典併爲每個值添加10。但是,當我運行此代碼時,出現錯誤,指出「材料未定義」。我該如何在兩個函數中正確使用字典?如何訪問字典中是在另一個函數定義在同一個班級

這裏是我的代碼:

class materialsClass: 
    def materialsChange(self): 
     while True: 
      q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ") 
      if q1 == 'edit': 
       while True: 
        q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ") 
        if q2 == 'add': 
         x = str(raw_input("Enter the Material: ")) 
         y = int(0) 
         Materials = {x:y} 
         break 
        elif q2 == 'edit': 
         x = str(raw_input("Enter your Material: ")) 
         y = int(raw_input("Enter your Change: ")) 
         Materials[x] += y 
         break 
        else: 
         print "Please Type an Option" 
      elif q1 == 'continue': break   
      else: 
       print "Type an Option!" 

     print Materials 


    def materialCounter(self): 
     for k in Materials: Materials[k] += 10 
     print Materials 

while True: 
    obj=materialsClass() 
    obj.materialsChange() 
    obj.materialCounter() 

回答

1

要建立在一些其他的答案。你必須在類下創建一個字典,並且你向字典添加項目的方式不正確,所以我改變了它。您還必須以正確的方式創建一個班級才能使其工作。無論如何,我已經檢查過這段代碼,它對我有用。我希望這有幫助。

class materialsClass(object): 
    def __init__(self): # create a new class 
     self.materials = {} # create a new dictionary under this class 
    def materialsChange(self): 
     while True: 
      q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ") 
      if q1 == 'edit': 
       while True: 
        q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ") 
        if q2 == 'add': 
         x = str(raw_input("Enter the Material: ")) 
         y = int(0) 
         self.materials[x] = y # this is how to add items to dictionaries sequentially 
         print self.materials 
         break 
        elif q2 == 'edit': 
         x = str(raw_input("Enter your Material: ")) 
         y = int(raw_input("Enter your Change: ")) 
         self.materials[x] = y 
         print self.materials 
         break 
        else: 
         print "Please Type an Option" 
      elif q1 == 'continue': break   
      else: 
       print "Type an Option!" 

     print self.materials 


    def materialCounter(self): 
     for k in self.materials: 
      self.materials[k] = self.materials[k] + 10 
     print self.materials 


obj=materialsClass() # you do not need to create the class again and again, just once is fine 

while True: 
    obj.materialsChange() 
    obj.materialCounter() 
3

你不能在另一個方法中使用一個局部變量的方法。您需要在類範圍中定義變量並將其變爲實例變量。例如:

class materialsClass: 
    def __init__(self): 
     self.Materials = dict() 

    def materialsChange(self): 
     ... 
     self.Materials[x] = y 
     (in place of Materials = {x:y}) 

    def materialCounter(self): 
     for k in self.Materials: 
      self.Materials[k] += 10 
     print self.Materials 

還要注意,在翻譯時運行線

Materials = {x:y} 

它取代了材料字典,一個新的,在詞典中,你實際上並沒有添加新的材料。這就是爲什麼你應該寫:

self.Materials[x] = y 

改爲。這會爲字典添加一個新的材料。

2

功能內部變量爲本地命名空間,所以你不能使用Materials第二個函數中,因爲它已經在第一個被定義!你可以在封閉類的範圍內,而不是最初Materials

但是請注意,你需要草簽__init__函數內部變量:

class materialsClass: 
    def __init__(self): 
     self.Materials=dict() 
    def materialsChange(self): 
     while True: 
      q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ") 
      if q1 == 'edit': 
       while True: 
        q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ") 
        if q2 == 'add': 
         x = str(raw_input("Enter the Material: ")) 
         y = int(0) 
         self.Materials = {x:y} 
         break 
        elif q2 == 'edit': 
         x = str(raw_input("Enter your Material: ")) 
         y = int(raw_input("Enter your Change: ")) 
         self.Materials[x] += y 
         break 
        else: 
         print "Please Type an Option" 
      elif q1 == 'continue': break   
      else: 
       print "Type an Option!" 

     print self.Materials 


    def materialCounter(self): 
     for k in self.Materials: self.Materials[k] += 10 
     print self.Materials 
+0

當我運行程序我收到指出NameError錯誤:不定義名稱「自我」 – user2757442 2015-02-07 17:58:17

+1

@ user2757442哎呀,你不要有'__init__'檢查出編輯 – Kasramvd 2015-02-07 18:07:42

相關問題