2015-08-15 140 views
0

我知道这个主题出现了很多,但是我很难将所读到的内容应用于我的具体情况。我正在学习Python的初学者课程,没有计划真正成为程序员,因为坦率地说,我很喜欢这个。请温柔? 我的错误:NameError:名称'_color_name'未定义

NameError:名字 '_color_name' 没有定义

我的代码:

class Project: 
    _number_of_colors = 0 
    _colors = [None for x in range(MAX_BEAD_COLORS)] 
    _total_beads = 0 
    _total_beads_metallic = 0 

    def get_colors(self): 
     done = False 
     color = None 
     has_metallic_beads = False 

     while not done: 
      has_metallic_beads = y_or_n("Is the color metallic (Y/N)? ") 
      if(has_metallic_beads): 
       color = MetallicColor() 
      else: 
       color = Color() 
      color.input() 
      self._colors[self._number_of_colors] = color 
      self._number_of_colors = self._number_of_colors + 1 
      done = y_or_n("Are there more colors in your project (Y/N)? ") 

    def display(self): 
     counter = 0 
     percent_metallic = 0.0 
     percent_color = 0.0 
     color = None 

     while counter < self._number_of_colors: 
      color = self._colors[counter] 
      percent_color = 100 * color.get_total_beads()/_total_beads 
      print("Color", (counter + 1), "-", percent_color, "of total:", color.get_total_beads(), "beads of", color.get_color(), ".") 
      counter = counter + 1 
     percent_metallic = 100 * self._total_beads_metallic/self._total_beads 
     print("Total Beads in Project:", _total_beads) 
     print("Total Metallic Beads in Project:", _total_beads_metallic) 
     print("Total percentage of Metallic Beads:", "{:.2f}".format(percent_metallic_beads)) 

    def calculate(self): 
     counter = 0 
     ingredient = None 

     while counter < self._number_of_ingredients: 
      ingredient = self._ingredients[counter] 
      self._total_beads = self._total_beads + color.get_qty_beads() 
      self._total_beads_metallic = self._total_beads_metallic + color.get_total_beads_metallic() 
      counter = counter + 1 

class Color: 
    _color_name = "" 
    _qty_beads = 0 

    def input(self): 
     self._color_name = get_string("What is the name of the color? ") 
     self._qty_beads = get_real("How many " + _color_name + " does your project require? ") 

    def get_color_name(self): 
     return self._color_name 

    def get_qty_beads(self): 
     return self._qty_beads 

class MetallicColor(Color): 
    def input(self): 
     self._color_name = get_string("What is the name of the color? ") 
     self._qty_beads = get_real("How many " + _color_name + " does your  project require? ") 


def project(): 
    project = None 

    project = Project() 
    project.get_colors() 
    project.calculate() 
    project.display() 
project() 

我已删除了部分验证和提示代码,以减少后期的大小。

+1

您应该包含整个回溯。它有很多有用的东西,比如行号。这就是说,你的问题是你没有使用'self._color_name'。 – Cyphase

+1

您还应该包括* all *的相关代码,您发布的代码中没有定义很多东西,比如'get_string','y_or_n','MAX_BEAD_COLORS' ... – alfasin

+0

谢谢您的提示!我认为太多的代码会是一个刺激。我将在未来包含所有内容。 – AmyIsabella

回答

2
class MetallicColor(Color): 
    def input(self): 
     self._color_name = get_string("What is the name of the color? ") 
     self._qty_beads = get_real("How many " + _color_name + " does your  project require? ") 

“多少”+ 自我。 _color_name

+0

打败我:P。 – Cyphase

+0

哦,我的天啊。谢谢!!我很尴尬,因为整个过程对我来说都很奇怪,但我无法弄清楚为什么。我需要坚持网页设计,哈! 现在到下一个错误!再次感谢! – AmyIsabella