私は一般的にプログラミングに非常に慣れていません。これは宿題のように見えると確信していますが、誰かのためである可能性がありますが、私は自分自身を教えているので、「自己宿題」ですか?
とにかく、タートルがランダムに四角形を作って窓を離れた回数を数えたいと思います。また、画面を終了するすべてのポイントにドットを配置したかったのですが、それは私自身の楽しみのためだけです。
毎回 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()
アドバイスをいただければ幸いです。