每1000步后打印值

时间:2015-01-29 06:08:20

标签: python python-3.x subprocess ipython

我想在最后一行代码的每一个间隔(1000)之后打印值,而不是每一个值。

DARTS=200000
hits = 0
throws = 0
rangen = RanGenerator()
pi = 0
avg = 0
mu = 0
var = 0
dev = 1

for i in range (1, DARTS):
    throws += 1
    x = rangen.rand()
    y = rangen.rand()
    z = rangen.rand()
    tt = x**2 + y**2 + z**2
    dist = sqrt(tt)
    if dist <= 1.0:
        hits = hits + 1.0
        pi = 6 * (hits / throws)
        avg = avg + pi
        mu = avg/throws
        var = (var+(mu-pi)**2)/throws
        dev = sqrt(var)
        print("%d: %s" % (i,dev))

1 个答案:

答案 0 :(得分:4)

使用模运算符很容易 - 只有i可被1000整除时才会打印值:

if i % 1000 == 0:
    print("%d: %s" % (i,dev))