2015-08-15 136 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