【发布时间】:2012-12-22 17:52:09
【问题描述】:
我的代码有另一个问题。 我正在用 Vpython 编写我的第一个程序,我必须模拟混合两种气体。首先,我遇到了边界问题,但是现在当球(代表气体粒子)停留在边界内时,就会出现不同的错误。几秒钟后,我收到一个错误,显示在我的函数源代码下方。
代码:
def MovingTheBall(listOfBalls,position,numCell,flagOfExecution):
flag = 0
if flagOfExecution==0:
positionTmp = position
else:
positionTmp = (position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0)
for i in range( 0, len(listOfBalls) ):
if positionTmp==listOfBalls[i].pos:
flag=1
if flag==1:
return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
else:
if positionTmp[0]==0 or positionTmp[0]>=numCell or positionTmp[0]<=-numCell or positionTmp[1]>=numCell or positionTmp[1]<=-numCell:
return MovingTheBall(lista,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
return positionTmp
错误是:
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 138, in MovingTheBall
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 138, in MovingTheBall
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 138, in MovingTheBall
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 138, in MovingTheBall
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 138, in MovingTheBall
return MovingTheBall(listOfBalls,(position[0]+choice([-1,0,1]),position[1]+choice([-1,0,1]),0),numCell,1)
File "gaz.txt", line 130, in MovingTheBall
if positionTmp==listOfBalls[i].pos:
RuntimeError: maximum recursion depth exceeded while calling a Python object
有人能想出一种方法来简化我的功能吗?
我在while循环中运行它:
while 1:
rate(20)
for i in range(0,len(self.listOfBalls)):
self.listOfBalls[i].pos=poruszanie(self.listOfBalls,self.listOfBalls[i].pos,self.numCell,0)
【问题讨论】:
-
除此之外也许,但一般来说,为变量和函数使用英文名称是一种很好的做法。你永远不知道谁会读你的代码——比如 Stackoverflow 用户 :-)
-
如果您解释一下您对这段代码打算做什么的思考过程,这可能会有所帮助。也许英文变量名就足够了,但也许不是。
-
我已经编辑了帖子,你现在可以帮我吗?您可能已经注意到英语不是我的第一语言。
-
附带说明,您的
for i in range(0,len(listOfBalls))块可以重写为:flag = any(positionTmp==i.pos for i in listOfBalls)
标签: python recursion runtime-error simulation vpython