我想回圈一個函式,直到串列中的所有元素都具有特定值,但我不知道如何實作此結果。
基本上,我正在根據 API 呼叫創建一個串列,我需要檢查所有元素是否都“成功”,如果不是,我想再次運行該函式,直到所有元素都處于成功狀態并繼續我的腳本。
串列示例:
list = ['init', 'init', 'success'] - need to repeat the function
list = ['init', 'success', 'success'] - need to repeat the function
list = ['success', 'success', 'success'] - Ready to go! continue the script
基本上,我在回圈中掙扎,然后呼叫函式。
任何人都可以幫助我嗎?
干杯。
uj5u.com熱心網友回復:
一種可能的方法是使用 while 回圈,直到函式回傳所需的結果。
myList = ['init']
while any(it != 'success' for it in myList):
my_list = functionCall()
uj5u.com熱心網友回復:
太好了,感謝您提供有關 any() 方法的提示。這正是我所需要的!
check_list = []
for a in sa_response_json['items']:
check_list.append(a['status']['state'])
print(f"-------------------- {check_list}")
if any(it != 'success' for it in check_list):
print("Checking devices status = Success. It can take some time")
sleep(10)
print("Checking again")
check_agent_state()
else:
print("-------------------- All devices have been onboarded")
print(f"-------------------- {check_list}")
uj5u.com熱心網友回復:
也許這樣的事情會奏效:
results = ("", "", "")
while result != ("success", "success", "success"):
results = function(x)
假設該函式回傳一個具有三個status的元組。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/372438.html
