在条件定义之前开始Python'while'循环

18 浏览
0 Comments

在条件定义之前开始Python'while'循环

这个问题已经在这里有了答案

我们可以在条件语句中进行赋值吗?

如何模拟do-while循环?

我有一段代码,它执行一个函数,然后根据输出决定是否重新运行:

while (function_output) > tolerance:
    function_output = function(x)

问题是,while循环不会开始直到 \"function_output\" 被定义 - 但它是在循环中定义的。目前我有:

function_output = function(x)
while (function_output) > tolerance:
    function_output = function(x)

但是有没有一种方法可以让这个循环在不必先迭代一次函数的情况下开始?

admin 更改状态以发布 2023年5月19日
0
0 Comments

使用break语句来从循环中跳出。 \n

while True:
    if function_output(x) > tolerance:
        break

0