gpt4 book ai didi

python - 如何在 python 编写的 while 语句中获取返回值?

转载 作者:行者123 更新时间:2023-12-01 04:02:45 26 4
gpt4 key购买 nike

我有一个函数来确定 UpdateStatus:

def detemUpStatus(localTuple, newTuple):
# some logic...
# it would return integer which would get processed later
return int(some number predefined)

detemUpStatus如果我们不需要更新,则会返回 0,所以我决定将其放在 while 中声明如下:

while (returnValue = detemUpStatus(lT, nT)):
# some logic to do process update
# with returnValue...

返回

while (i = 2):
^

SyntaxError: invalid syntax

我对 python 还很陌生,我认为应该有一种优雅的方式,比如 C语言确实如此。

最佳答案

我认为您无法在 while 子句中进行赋值。一些替代方案:

returnValue = detemUpStatus(lT, nT)
while returnValue:
...
returnValue = detemUpStatus(lT, nT)

或者也许:

while True:
returnValue = detemUpStatus(lT, nT)
if not returnValue:
break
...

关于python - 如何在 python 编写的 while 语句中获取返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com