저는 일반적으로 프로그래밍에 익숙하지 않습니다. 숙제와 같아 보이지만, 누군가를위한 것 같지만 스스로 가르치기 때문에 "스스로 숙제"가됩니까?파이썬에서 함수 내에서 조건이 충족되는 시간은 어떻게 계산합니까?
어쨌든 거북이가 무작위로 사각형을 만들 때 창이 떠나는 횟수를 계산하고 싶습니다. 나는 또한 그것이 화면을 빠져 나가는 모든 지점에 점을 찍고 싶었지만 그것은 단지 나의 재미를위한 것입니다.
나는 매번 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 이후 드로잉 루프가 끝나거나 끝나지 않습니다. – theSchap