在保留两个for循环的条件下,怎么让柱状图正常画出
import turtle# Function that draws the turtle
def drawBar(t, height, color):
# Get turtle t to draw one bar
# of height
# Start filling this shape
t.fillcolor(color)
t.begin_fill()
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
# stop filling the shape
t.end_fill()
# Driver Code
s_list=
for i in range(7):
count = s_list
xs =
clrs = ["green", "red", "yellow", "black",
"pink", "brown", "blue"]
maxheight = max(xs)
numbers = len(xs)
border = 100
# Set up the window and its
# attributes
wn = turtle.Screen()
wn.setworldcoordinates(0 - border, 0 - border,
40 * numbers + border,
maxheight + border)
tess = turtle.Turtle()
tess.pensize(3)
for x in range(len(xs)):
drawBar(tess, xs,
clrs) 在你的代码中,绘制柱状图的部分有一些问题。首先,你应该使用循环变量 `i` 来获取每个元素的高度和颜色,而不是直接使用列表本身。另外,在绘制每个柱状图时,你应该使用索引 `x` 来获取对应的高度和颜色。
以下是修改后的代码:
import turtle
# Function that draws the turtle
def drawBar(t, height, color):
# Get turtle t to draw one bar
# of height
# Start filling this shape
t.fillcolor(color)
t.begin_fill()
t.left(90)
t.forward(height)
t.write(str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.left(90)
# stop filling the shape
t.end_fill()
# Driver Code
s_list =
clrs = ["red", "green", "blue", "yellow", "orange", "purple", "pink"]
maxheight = max(s_list)
numbers = len(s_list)
border = 100
# Set up the window and its attributes
wn = turtle.Screen()
wn.setworldcoordinates(0 - border, 0 - border,
40 * numbers + border,
maxheight + border)
tess = turtle.Turtle()
tess.pensize(3)
for x in range(len(s_list)):
drawBar(tess, s_list, clrs)
turtle.done()
希望这可以解决你的问题!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]