总的来说,我对编程很陌生,虽然我确信这看起来像是家庭作业,但它很可能是为某人准备的,但我是在自学,所以这是“自我作业”?在
不管怎么说,我想数一数乌龟离开窗户的次数,因为它随机地制造了正方形。我还想在它离开屏幕的每一点上都画上一个点,但那只是为了我自己的乐趣。在
我知道我每次都设置为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()
如有任何建议,将不胜感激。在
把引用特定事物的变量放入一个类中怎么样?在
您可以返回一个列表对象,该对象具有“stillIn”值和累加器的值。在
最简单的方法是跟踪您的海龟在您的函数之外,但在您的
while
循环内离开屏幕的次数。在不要让您的函数返回海龟是否已经退出四次,只要它在该步骤中退出就返回。你必须改变你的功能,看起来像:
然后,您可以跟踪您在
^{pr2}$while
循环中外出的次数:相关问题 更多 >
编程相关推荐