2015-02-07 14 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

相关问题