2017-02-16 16 views
1

私はPythonでプログラミングを学んでいます(考えてみるとpython 2)、プログラムで打ちました。問題文:Python program to draw a symmetric flower after seeking the size of and number of petals from userpython turtleで花を描く

私は次のコードは、私は各花弁の間に角度を得ることができないことを除いて右(一部のコードは、最後の部分の近くにある状態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() 

Expected Flower 正しい(対称) Incorrect Flower 誤った(非対称)

+0

1)あなたがインポートしたタートルパッケージのドキュメントへのリンクを付けることはできますか? 2)発生した結果と期待していた結果の間にどのような違いがありますか?私。あなたは花びんの間の角度を数学的に正しくしていないことをどうやって知っていますか?グラフィックを表示するのに役立ちます。 – LarsH

+0

私は最終的に予想される出力のスナップで元の質問を更新しました...花は対称でなければなりません。公式の文書はここにあります - > https://docs.python.org/3.1/library/turtle.html – njathan

答えて

2

はちょうどこのようなあなたのコードを変更(draw_petalsb.rt(360/petals-30、正しいbob.lt(360/petals)360/4に追加):

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) 
    b.rt(360/petals-30) # this will take care of the correct angle b/w petals 


import turtle 
bob=turtle.Turtle() 
#draw_petal(bob,radius) 

for i in range(petals): 
    draw_petal(bob,radius) 
    bob.lt(360/4) 
+1

'bob.lt(360/4)'行を修正するのではなく、その行を維持するという正当な理由はありません'bob.lt(360/petals)'になるようにしました。 'draw_petal()'のコードを修正して、実際に修正するのではなく、別の場所にエラーを残して保存するようです。 – cdlane

+0

@cdlaneを指摘してくれてありがとう。私は4枚の花びらで試していました。あなたは私の中で正しく修正しました。私の意図は 'bob.lt(360/petals)' – njathan

+0

Thanks @ Tarptaeyaです。オリジナルの投稿を誤っていると考えて、あなたのソリューションは4枚の花びらに完全に対応しています..元の質問を今修正してください... – njathan

1

I問題はあなたが作っているよりも簡単だと思う。

最初の問題は、花びらを描くと、亀の見出しが変わり、数学をやって、開始位置に戻すことです。ここでは、花びらを描く前に見出しを記録し、後でそれを復元します。数学はありません。

第二の問題は、亀は非常に速く、同じ結果を生成しますが、turtle.circle()程度引数を使用してこれを行うことができたときにあなたがあなた自身のアークコードを実装しています:

from turtle import Turtle, Screen 

def draw_petal(turtle, radius): 
    heading = turtle.heading() 
    turtle.circle(radius, 60) 
    turtle.left(120) 
    turtle.circle(radius, 60) 
    turtle.setheading(heading) 

my_radius = int(input("What is the radius of the flower? ")) 
my_petals = int(input("How many petals do you want? ")) 

bob = Turtle() 

for _ in range(my_petals): 
    draw_petal(bob, my_radius) 
    bob.left(360/my_petals) 

bob.hideturtle() 

screen = Screen() 
screen.exitonclick() 

USAGE

> python3 test.py 
What is the radius of the flower? 100 
How many petals do you want? 10 

OUTPUT

enter image description here

+0

親愛なる@cdlane、OP doesn '元のコード – Tarptaeya

+0

@ OPを修正したい代わりの解決策が必要な場合、元のコードへの修正は 'draw_petal()'の 'heading = turtle.heading()'と 'turtle.setheading(heading)'です。受け入れられた答えでなければなりません。他の改善点も貴重です。 – LarsH