2017-11-01 9 views
0

私は、測定カップに似た三角形の四角形を描く関数cup()を持っています。私はそのコードを正しく書いたと思いますが、それを呼び出す関数cups()は正しく表示されません。誰も私の問題が何かを見ることができますか?私はそれが進む長さを変更しようとしましたが、それは何もしませんでした。それは最初の広場の上端で終わる。助けてくれてありがとう。Pythonカメのネストされた四角形を描く

def cup(t,sideLength): 
    for i in range(3): 
     t.forward(sideLength) 
     t.left(90) 
     t.pu() 
     t.forward(sideLength) 
     t.left(90) 
     t.pd() 

def cups(t,initial,incr,reps): #needs work 
    '''calls the function cup repeatedly to draw a set of measuring cups of 
    increasing size.''' 
    for i in range(reps): 
     cup(t,initial) 
     t.pu() 
     t.right(90) 
     t.fd(incr) 
     t.lt(90) 
     t.pd() 
     initial += incr 

import turtle 
s = turtle.Screen() 
t = turtle.Turtle() 
cups(t, 50, 30, 4) 

My code

Correct code

答えて

0

コードをいくつかちゃったごめんなさいがありました。

機能カップが複雑すぎた - 常に基本的なものを最初にテストする - それを行うには2つの方法があり、1つには範囲と1つがありません。私は簡単にするために1つをしなかった。 次に、カメがどこで終わり、ステップを増やすのかを考えていますが、中央のコンテナから均等に移動するには、増加分を上半分と下半分に分けて配分することを考える必要があります。

import turtle 

t = turtle.Turtle() 

def cup(sideLength): 
    """Draws one cup and returns to origin""" 
    t.pd() 
    t.forward(sideLength) 
    t.left(90) 
    #t.pu() 
    t.forward(sideLength) 
    t.left(90) 
    t.forward(sideLength) 
    t.pu() 
    t.left(90) 
    t.forward(sideLength) 

def cups(initial,incr,reps): #needs work 
    '''calls the function cup repeatedly to draw a set of measuring cups of increasing size.''' 
    start = initial 
    for i in range(reps): 
     cup(start) 
     t.pu() 
     t.forward(incr/2) 
     t.left(90) 
     start += incr 

#cup(50) 
cups(50, 30, 4) 

コードの流れに従って、各行が何をしているのかを理解するためにコメントを追加します。これがあなたの質問に答えるならば、これを受け入れられた解決策としてマークすることができます。

+1

ありがとうございます! – user8742923

+0

cdlaneに問題ありません。コードが何をしているのか理解できることを願っています。新しい言語で始めると読みやすく、わかりやすいコード行があります。ショートコードよりも読みやすいです。imho :)正規表現をチェックする(正規表現)ifあなたは1行のコードの答えをしたい。がんばろう! – srattigan

0

あなたcup()機能は、正しい手順を持っているが、残念ながらループの後に来る必要がありますループ内でいくつかを置く:あなたはカップの間を移動どのくらいのプラス少し調整があなたの問題を解決すること

from turtle import Turtle, Screen 

def cup(turtle, sideLength): 
    for _ in range(3): 
     turtle.forward(sideLength) 
     turtle.left(90) 
    turtle.penup() 
    turtle.forward(sideLength) 

def cups(turtle, initial, incr, reps): 
    ''' 
    Calls the function cup() repeatedly to draw 
    set of measuring cups of increasing size. 
    ''' 

    for length in range(0, incr * reps, incr): 
     cup(turtle, initial + length) 
     turtle.forward(incr/2) 
     turtle.left(90) 
     turtle.pendown() 

screen = Screen() 
tortoise = Turtle() 

cups(tortoise, 50, 30, 4) 

screen.exitonclick() 

cups()機能でfor ... in range(...)を使用する別の方法を示しました。私はまた、cup()を少しでも意識して、余分な回りを避け、図を滑らかな動きで描くようにしました。

+0

ありがとうございました! – user8742923

関連する問題