I am modeling a particle moving through different layers, and once it moves through the layer I want it to print a few things. My problem is the particle can move back into the previous layer and come out again, which triggers it to print again, I do not want this.
while math.sqrt((pho.pos.x)*(pho.pos.x)+(pho.pos.y)*(pho.pos.y)+(pho.pos.z)*(pho.pos.z))<10:
x=random.uniform(-j,j)
y=random.uniform(-j,j)
z=random.uniform(-j,j)
step=(x,y,z)
t=t+dt
pho.pos=pho.pos+step
print 'Step Number', t
rate(speed)
d=math.sqrt((pho.pos.x)*(pho.pos.x)+(pho.pos.y)*(pho.pos.y)+(pho.pos.z)*(pho.pos.z))
'''if d>10:
print pho.pos
print d
print 'Out of Layer 1 in',t,'steps!'
break
else:
pass'''
d=math.sqrt((pho.pos.x)*(pho.pos.x)+(pho.pos.y)*(pho.pos.y)+(pho.pos.z)*(pho.pos.z))
print pho.pos
print d
print 'Out of Layer 1 in',t,'steps!'
The part I don't want repeating is the last three print statements, I have tried break statements but this code is a nested loop, and when it gets re-looped it starts over before the break.
On whatever class pho
is, make a property called printed
or something and initialize it to False
. Then, set it to True when printing for the first time, and say
if d > 10 and not pho.printed
instead of just if d > 10
.
Try something like this. Your code I think was all supposed to be part of the while loop. If not, move the out_of_layer
to before the next outer loop.
out_of_layer = False
while math.sqrt((pho.pos.x)*(pho.pos.x)+(pho.pos.y)*(pho.pos.y)+(pho.pos.z)*(pho.pos.z))<10:
x=random.uniform(-j,j)
... blah blah stuff you already know about ...
d=math.sqrt((pho.pos.x)*(pho.pos.x)+(pho.pos.y)*(pho.pos.y)+(pho.pos.z)*(pho.pos.z))
if not out_of_layer:
print pho.pos
print d
print 'Out of Layer 1 in',t,'steps!'
out_of_layer = True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.