一般来说,我对编程很陌生,虽然我确定这看起来像是家庭作业,但可能适合某人,但是我在教自己,所以这是“自我作业”吗?
无论如何,我想计算一下乌龟随机成正方形时离开窗户的次数.我还想在退出屏幕的每个点上都放一个点,但这只是为了我自己的乐趣.
我知道每次都将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()
任何意见,将不胜感激.