0
我是一個初學者在python和我正在寫一個簡單的程序與蟒蛇龜,提示用戶輸入一個多邊形的邊長,程序應該繪製多邊形並在多邊形下打印出人物(我)的名字。如何對齊蟒蛇與n邊繪製多邊形中的文本
我已經得到程序的工作,但我似乎無法弄清楚如何讓文字在多邊形下打印,因爲邊長可以由用戶改變,所以多邊形有時可以看不見,取決於邊長的估算值。
的程序應該是這樣的: My result
我的代碼如下: end result
然而,我的每一次看起來有點像這個
import turtle
print('************************************************')
print('This program draws a randomly colored polygon')
print('with side lengths provided by the user.')
print('************************************************')
polygonSideLength = int(input('Enter length of polygon side: \n'))
numberOfSides = int(5 + (28/4))
turnAngle = 360/numberOfSides
import random
randomColor = random.randint(0,5)
if randomColor == 0:
fillcolor="red"
elif randomColor == 1:
fillcolor="green"
elif randomColor == 2:
fillcolor="blue"
elif randomColor == 3:
fillcolor="cyan"
elif randomColor == 4:
fillcolor="magenta"
elif randomColor == 5:
fillcolor="yellow"
print('Length of polygon side =', polygonSideLength)
print('Number of polygon sides =', numberOfSides)
print('Turn angle at each vertex =', turnAngle)
print('Random fill color is', fillcolor)
turtle.begin_fill()
turtle.pen(pensize = 5, pencolor="black", fillcolor = fillcolor)
count = 0
while (count < numberOfSides):
turtle.forward(polygonSideLength)
turtle.right(turnAngle)
count = count + 1
turtle.end_fill()
turtle.setheading(270)
turtle.penup()
turtle.forward(65)
turtle.left(90)
turtle.forward(130)
turtle.pendown()
turtle.write("polygon drawn by: Name", align = "right", font=("Arial", 12, "normal"))
turtle.hideturtle()
turtle.done()
能有人幫助我如何更改此代碼,以便它能正常工作?