3

一般来说,我对编程很陌生,虽然我确信这看起来像是家庭作业,但它可能是给某人的,但我正在自学,所以这是“自学作业”?

无论如何,我想计算一只乌龟在随机形成正方形时离开窗户的次数。我还想在它退出屏幕的每个点上放一个点,但这只是为了我自己的乐趣。

我知道我每次都设置为 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循环内部离开屏幕的次数。

不要让你的函数返回乌龟是否已经出去了四次,如果它在那一步出去了就让它返回。你必须改变你的功能看起来像:

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

一种方法是只创建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

基本上,您替换outs整个isInScreen.outs函数体,然后在定义函数后对其进行初始化。(不幸的是,你不能初始化函数内部的值,或者每次调用它时它都会被重置。)

请注意,这不是一个常见的习语。大多数时候,您将拥有一个具有outs属性的类和isInScreen一个更新属性的方法。

于 2012-07-17T19:08:26.347 回答
0

您可以返回一个列表对象,该对象具有“stillIn”值以及累加器的值。

于 2012-07-17T18:37:50.180 回答