我想做类似的事情:
def myfunc(p1, p2, p3):
time_step = 0
if p1 <= 0
while (p2 > 0 or p3 > 0)
stuff that updates p2, p3
timestep+=1
else
while (time_step < p1)
stuff that updates p2, p3
timestep+=1
Run Code Online (Sandbox Code Playgroud)
基本上,我希望能够让用户决定他们是否希望 while 循环运行直到 p2 和 p3 小于或等于 0,或者他们是否希望 while 循环运行到所需的 time_step。在任何一种情况下,“更新 p2、p3 的东西”都是完全相同的。但是,while 循环中的内容由很多行组成,我只会复制和粘贴“更新 p2、p3 的内容”。我觉得一定有更好的方法。
我希望以下方法可行:
def myfunc(p1, p2, p3):
time_step = 0
if p1 <= 0
conditional_statement = (p2 > 0 or p3 > 0)
else
conditional_statement = (time_step < p1) …Run Code Online (Sandbox Code Playgroud)