我正在學習用python編程(指的是想想python 2
),並且對程序感到驚訝。問題說明:Python program to draw a symmetric flower after seeking the size of and number of petals from user
。用蟒蛇繪製花朵
我想出的代碼如下,除了我無法得到每個花瓣之間的數學角度正確的部分(代碼附近的代碼狀態bob.lt(360/petal)
)。有人可以幫忙嗎?
import math
radius=int(input("What is the radius of the flower? "))
petals=int(input("How many petals do you want? "))
#radius=100
#petals=4
def draw_arc(b,r): #bob the turtle,corner-to-corner length (radius) of petal (assume 60 degree central angle of sector for simplicity)
c=2*math.pi*r #Circumference of circle
ca=c/(360/60) #Circumference of arc (assume 60 degree central angle of sector as above)
n=int(ca/3)+1 #number of segments
l=ca/n #length of segment
for i in range(n):
b.fd(l)
b.lt(360/(n*6))
def draw_petal(b,r):
draw_arc(b,r)
b.lt(180-60)
draw_arc(b,r)
import turtle
bob=turtle.Turtle()
#draw_petal(bob,radius)
for i in range(petals):
draw_petal(bob,radius)
bob.lt(360/petals)
turtle.mainloop()
正確(對稱) 不正確的(非對稱)
1)你可以鏈接到你導入的Turtle包的文件嗎? 2)你看到發生的結果和你期望的結果有什麼不同?即你怎麼知道你沒有在數學上得到花瓣之間的角度?這可能有助於顯示圖形。 – LarsH
我已經用最後期望的輸出捕捉來更新原始問題......花應該是對稱的。官方文檔在這裏 - > https://docs.python.org/3.1/library/turtle.html – njathan