Python 名称错误:名称'_颜色名称';没有定义
我知道这个话题经常出现,但是我很难将我读到的应用到我的具体情况中。我正在学习一门非常初级的Python课程,没有打算成为一名真正的程序员,因为坦率地说,我在这方面很差劲。请温柔点? 我的错误: 名称错误:未定义名称“\u颜色\u名称” 我的代码:Python 名称错误:名称'_颜色名称';没有定义,python,Python,我知道这个话题经常出现,但是我很难将我读到的应用到我的具体情况中。我正在学习一门非常初级的Python课程,没有打算成为一名真正的程序员,因为坦率地说,我在这方面很差劲。请温柔点? 我的错误: 名称错误:未定义名称“\u颜色\u名称” 我的代码: class Project: _number_of_colors = 0 _colors = [None for x in range(MAX_BEAD_COLORS)] _total_beads = 0 _total_
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()
类项目:
_颜色的数量=0
_颜色=[范围内x无(最大颜色)]
_总数=0
_总珠粒数=0
def get_颜色(自身):
完成=错误
颜色=无
有金属珠=假
虽然没有这样做:
是否有金属珠=y或n(“颜色是金属的(y/n)”)
如果(有金属珠):
颜色=金属色()
其他:
颜色=颜色()
color.input()
self.\u颜色[self.\u颜色数]=颜色
self.\u颜色的数量=self.\u颜色的数量+1
done=y\u或n(“您的项目中是否有更多颜色(y/n)?”)
def显示(自):
计数器=0
金属百分比=0.0
颜色百分比=0.0
颜色=无
当计数器
我删除了一些验证和提示代码,以减少帖子的大小
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? ")
“多少”+自我。\u color\u name您应该包括整个回溯。它有一些有用的东西,比如行号。也就是说,你的问题是你没有使用
self.\u color\u name
。你还应该包括所有相关的代码,你发布的代码中有很多东西没有定义,比如get\u string
,y\u或\u n
,MAX\u BEAD\u COLORS
…谢谢你的提示!我认为太多的代码会让人恼火。我会在将来把所有这些都包括进去。非常感谢。我很尴尬,因为我一直觉得这很奇怪,但我不知道为什么。我需要坚持网页设计,哈!现在转到下一个错误!再次感谢!