在python中,如何计算函数中满足条件的次数?

时间:2012-07-17 18:32:58

标签: python

我对编程很新,虽然我确定这看起来像是家庭作业,但它可能适合某人,但我在教自己,所以这是“自学作业”?

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

我知道我每次都会设置为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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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()

答案 2 :(得分:0)

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

答案 3 :(得分: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更新属性的方法。