3

私は一般的にプログラミングに非常に慣れていません。これは宿題のように見えると確信していますが、誰かのためである可能性がありますが、私は自分自身を教えているので、「自己宿題」ですか?

とにかく、タートルがランダムに四角形を作って窓を離れた回数を数えたいと思います。また、画面を終了するすべてのポイントにドットを配置したかったのですが、それは私自身の楽しみのためだけです。

毎回 outs を 0 に設定していることはわかっていますが、既に値を返す必要があるこのような関数内でアキュムレータ パターンを作成する方法がわかりません (それが正しい場合)。

これが私のコードです:

import random
import turtle

def isInScreen(w,t):

    leftBound = - w.window_width()/2
    rightBound = w.window_width()/2
    topBound = w.window_height()/2
    bottomBound = -w.window_height()/2

    turtleX = t.xcor()
    turtleY = t.ycor()


    stillIn = True
    outs = 0

    if turtleX > rightBound or turtleX < leftBound:
        t.dot()
        t.right(180)
        t.forward(50)
        outs += 1
        print(outs)
        return outs

    if turtleY > topBound or turtleY < bottomBound:
        t.dot()
        t.right(180)
        t.forward(50)
        outs += 1
        print(outs)
        return outs

    if outs == 4:
        stillIn = False

    return stillIn

t = turtle.Turtle()
wn = turtle.Screen()

t.shape('turtle')
while isInScreen(wn,t):
    coin = random.randrange(0,2)
    if coin == 0:
        t.left(90)
    else:
        t.right(90)

    t.forward(50)

wn.exitonclick()

アドバイスをいただければ幸いです。

4

4 に答える 4

3

whileこれを行う最も簡単な方法は、カメが関数の外で、ループの内側で画面から消えた回数を追跡することです。

カメが4回出た場合に関数を返すのではなく、そのステップで出た場合は関数を返すようにします。次のように関数を変更する必要があります。

def isScreen(w, t):
    if turtleX > rightBound or turtleX < leftBound:
        return True
    if turtleY > topBound or turtleY < bottomBound:
        return True
    else:
        return False

while次に、ループに出た回数を追跡できます。

outs = 0
while outs < 4:
    if isScreen:
        outs += 1
于 2012-07-17T18:45:59.283 に答える
1

特定のものを参照する変数をクラスに入れてみませんか?

class MyTurtle(object):

    def __init__(self):
        self.outs = 0

    def isInScreen(self, w, t):
        leftBound = - w.window_width()/2
        rightBound = w.window_width()/2
        topBound = w.window_height()/2
        bottomBound = -w.window_height()/2

        turtleX = t.xcor()
        turtleY = t.ycor()

        stillIn = True

        if turtleX > rightBound or turtleX < leftBound:
            t.dot()
            t.right(180)
            t.forward(50)
            self.outs += 1
            print(self.outs)
            return outs

        if turtleY > topBound or turtleY < bottomBound:
            t.dot()
            t.right(180)
            t.forward(50)
            self.outs += 1
            print(self.outs)
            return outs

        if self.outs == 4:
            stillIn = False

        # for some reason i think this line was missing
        return stillIn
        # or this 
        return outs


t = turtle.Turtle()
wn = turtle.Screen()

myThing = MyTurtle()
t.shape('turtle')

# now you know WHAT is located "in screen"
# and you could now have lots of turtlely
# things running off the screen too with a
# little modification where each "myturtle"
# keeps track of its own "outs"

while myThing.isInScreen(wn, t):
    coin = random.randrange(0,2)
    if coin == 0:
        t.left(90)
    else:
        t.right(90)
    t.forward(50)
wn.exitonclick()
于 2012-07-17T20:43:09.487 に答える
0

1 つの方法はouts、グローバル変数を作成することです (推奨されません)。

outs = 0
def isInScreen(w,t):
    ...

カプセル化する少し良い方法outsは、それを関数自体の属性にすることです。このように、グローバル変数のように機能します。

def isInScreen(w,t):

    leftBound = - w.window_width()/2
    rightBound = w.window_width()/2
    topBound = w.window_height()/2
    bottomBound = -w.window_height()/2

    turtleX = t.xcor()
    turtleY = t.ycor()


    stillIn = True

    if turtleX > rightBound or turtleX < leftBound:
        t.dot()
        t.right(180)
        t.forward(50)
        isInScreen.outs += 1
        print(isInScreen.outs)
        return isInScreen.outs

     # rest of the function

isInScreen.outs = 0

基本的に、関数の本体全体で置換outsisInScreen.outs、関数が定義された後に初期化します。(残念ながら、関数内で値を初期化することはできません。または、呼び出すたびにリセットされます。)

これは一般的なイディオムではないことに注意してください。ほとんどの場合outs、属性としてのクラスと、属性isInScreenを更新するメソッドがあります。

于 2012-07-17T19:08:26.347 に答える
0

「stillIn」値とアキュムレータの値を持つリスト オブジェクトを返すことができます。

于 2012-07-17T18:37:50.180 に答える