|
发表于 2023-12-10 19:05:20
|
显示全部楼层
在你的代码中,绘制柱状图的部分有一些问题。首先,你应该使用循环变量 `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 = [10, 30, 20, 40, 50, 15, 25]
- 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[x], clrs[x])
-
- turtle.done()
复制代码
希望这可以解决你的问题!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|