在python中,如何计算函数内满足条件的次数?
我对编程一般都很陌生,虽然我确信这看起来像作业,但它可能是有人,但我在教自己,所以它是“自我作业”?在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()
任何意见,将不胜感激。
您可以返回一个列表对象,它具有'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
循环出去:
outs = 0
while outs < 4:
if isScreen:
outs += 1
一种方法是只是让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
基本上,你isInScreen.outs
更换outs
整个函数体,对其进行初始化函数定义之后。 (不幸的是,您无法初始化函数内部的值,或者每次调用它时都会重置它。)
请注意,这不是一个常见的习惯用法。大多数情况下,您将有一个类为outs
作为属性,而isInScreen
是更新属性的方法。
“不是一个普通的习惯用法”应该可能替换为“必然会迷惑读者”。正如PEP20所指出的那样:“如果实施难以解释,这是一个坏主意。”并且使用来自外部的函数名称空间甚至很难向解释器解释(这就是为什么需要“不幸”的警告)。 – msw 2012-07-17 19:59:58
如何将变量引用到一个具体的东西到类?
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()
对不起,但我似乎无法让您的答案正常工作。绘图循环在4之后结束,否则永不结束。 – theSchap 2012-07-18 00:32:53