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

2024-09-28 21:49:23 发布

您现在位置: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()

如有任何建议,将不胜感激。在


Tags: importrightreturnifrandomwindowforwardturtle
3条回答

把引用特定事物的变量放入一个类中怎么样?在

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()

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

最简单的方法是跟踪您的海龟在您的函数之外,但在您的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循环中外出的次数:

^{pr2}$

相关问题 更多 >