Python 3不需要的无限While循环

时间:2018-10-19 15:58:17

标签: python loops while-loop infinite

v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
    print(v,t)
    v0 = v
    t = t+dt

我的意图是,在第一次检查v> 0之后,在重新执行检查之前,我们使v0等于v。 取而代之的是,我看到的是第一个计算出的v = 0.9的无限流

2 个答案:

答案 0 :(得分:0)

您每次需要使用更新的参数在循环中重新计算v

import numpy as np

v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt

while v > 0:
    print(v,t)
    v0 = v
    t = t+dt
    v = v0 - alpha*np.sqrt(v0)*dt

输出:

0.9 0
0.805131670195 0.1
0.715402540577 0.2
0.630821068577 0.30000000000000004
0.551396823627 0.4
0.477140724644 0.5
0.408065350983 0.6
0.34418535795 0.7
0.28551804525 0.7999999999999999
0.232084156635 0.8999999999999999
0.183909043077 0.9999999999999999
0.141024425429 1.0999999999999999
0.103471206469 1.2
0.0713042659134 1.3
0.0446014072812 1.4000000000000001
0.0234823620184 1.5000000000000002
0.00815840625677 1.6000000000000003

答案 1 :(得分:0)

v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
    print(v,t)
    v0 = v
    t = t+dt
    v = v0 - alpha*np.sqrt(v0)*dt