我需要fillcolor来读取Python中的值
我正在设置两个定义,我希望从牵引杆读取fillColor。程序没有读取与该值对应的正确颜色我需要fillcolor来读取Python中的值,python,function,Python,Function,我正在设置两个定义,我希望从牵引杆读取fillColor。程序没有读取与该值对应的正确颜色 import turtle wn = turtle.Screen() # Set up the window wn.bgcolor("white") tess = turtle.Turtle() tess.penup() tess.goto(-100,-75) tess.pendown() def drawBar(t, height): """ Get turt
import turtle
wn = turtle.Screen() # Set up the window
wn.bgcolor("white")
tess = turtle.Turtle()
tess.penup()
tess.goto(-100,-75)
tess.pendown()
def drawBar(t, height):
""" Get turtle t to draw one bar, of height. """
t.left(90)
t.begin_fill()# Point up
t.forward(height)
# Draw up the left side
t.right(90)
t.forward(40) # width of bar, along the top
t.right(90)
t.forward(height)
t.end_fill()# And down again!
t.left(90)
def drawColor(t, height):
drawBar(t, height)
if height >= 200:
return tess.fillcolor("red")
elif height < 200 and v >= 100:
return tess.fillcolor("yellow")
elif height < 100:
return tess.fillcolor("green")
xs = [48, 117, 200, 240, 160, 260, 220]
for v in xs: # assume xs and tess are ready
drawColor(tess, v)
导入海龟
wn=turtle.Screen()#设置窗口
wn.bgcolor(“白色”)
苔丝=乌龟。乌龟()
苔丝·彭普
苔丝。后藤(-100,-75)
苔丝·彭顿()
def牵引杆(t,高度):
“让海龟t画一个高度的横杆。”
t、 左(90)
t、 开始填充()#指向上
t、 前进(高度)
#画左边
t、 右(90)
t、 向前(40)#钢筋宽度,沿顶部
t、 右(90)
t、 前进(高度)
t、 结束填充(),然后再次向下!
t、 左(90)
def drawColor(t,高度):
牵引杆(t,高度)
如果高度>=200:
返回tess.fillcolor(“红色”)
elif高度<200且v>=100:
返回tess.fillcolor(“黄色”)
elif高度<100:
返回tess.fillcolor(“绿色”)
xs=[4811720240160260220]
对于xs中的v:#假设xs和tess已经准备好了
drawColor(苔丝,v)
我不知道为什么这不起作用。我想当你提到
高度测试时,可能会有打字错误:
elif height < 200 and v >= 100:
return tess.fillcolor("yellow")
将drawColor
更改为:
def drawColor(t, height):
if height >= 200:
t.fillcolor("red")
elif height < 200 and height >= 100:
t.fillcolor("yellow")
elif height < 100:
t.fillcolor("green")
drawBar(t, height)
def drawColor(t,高度):
如果高度>=200:
t、 填充颜色(“红色”)
elif高度<200且高度>=100:
t、 填充颜色(“黄色”)
elif高度<100:
t、 填充颜色(“绿色”)
牵引杆(t,高度)
通过这种方式,您可以首先根据当前高度设置正确的颜色,然后绘制条形图。在原始代码中,您正在使用当前颜色(从默认颜色黑色开始)绘制一个条形图,然后更改要绘制的颜色,这样每个新条形图都将以最后一个条形图应有的颜色绘制
在您的原始代码中还存在一些其他问题。您不使用传递的海龟对象t
,而是使用全局对象tess
。也不需要返回fillcolor
调用的结果
def drawColor(t, height):
drawBar(t, height)
if height >= 200:
# return tess.fillcolor("red")
return t.fillcolor("red")
...
def drawColor(t, height):
if height >= 200:
t.fillcolor("red")
elif height < 200 and height >= 100:
t.fillcolor("yellow")
elif height < 100:
t.fillcolor("green")
drawBar(t, height)