2024-04-20 03:03:43 发布
网友
我不建议使用while循环,因为它不是pythonic。我做了一些研究,我了解大多数情况。我发现如果有人能帮我的话,我很难想出一个替代的方法。你知道吗
time = getTime() while time > dt: doSth(dt) time -= dt
你怎么能把它改成一种更像Python的方式呢?你知道吗
for i in range(time//dt): doSth(dt)
这个怎么样?你知道吗
您可以在for循环中迭代如下所示的range对象,尽管while循环没有任何非Pythonic的内容:
for
while
for time in range(getTime(), 0, -dt): doSth(dt) # or perhaps your mean to call doSth(time), since dt does not change
或者,如果时间戳是浮点数:
for i in range(getTime() // dt): doSth(getTime() - i * dt)
您可以在
for
循环中迭代如下所示的range对象,尽管while
循环没有任何非Pythonic的内容:或者,如果时间戳是浮点数:
相关问题 更多 >
编程相关推荐