我想解決'Think Python'第5章的練習9(版本:http://openbookproject.net/thinkcs/python/english3e/conditionals.html)。它包括從turtle.write中移動文本,以便在值爲負時與條形圖不重疊。爲了在文本之前添加額外的行,我試圖使用三重引號「」「像這樣的」「」,但額外的行進入錯誤的地方。請幫忙?如何在Python中使用turtle.write時移動文本?
import turtle
wn=turtle.Screen()
wn.bgcolor("lightgreen")
wn.title("Barcharts FTW")
pen=turtle.Turtle()
pen.hideturtle()
pen.color("blue","red")
pen.pensize(2)
pen.penup()
pen.goto(-300,-100)
def draw_bar (t,height):
t.pendown()
t.begin_fill()
t.lt(90)
t.fd(height)
t.write(" " + str(height))
t.rt(90)
t.fd(40)
t.rt(90)
t.fd(height)
t.end_fill()
t.lt(90)
t.penup()
t.fd(10)
xs = [48, 117, 200, 240, -160, 260, 220]
for v in xs:
draw_bar(pen,v)
wn.mainloop()
每次運行時創建一個不同的條形圖,只是玩得開心! –