[英]Execute a function to return a value on a loop until that function returns False - Python
[英]Loop over a python function if it has a return value
如果函數有返回值,我會嘗試循環它。
所以基本上當我得到最新的訂單時,有時我會得到一個“下一個令牌”。 如果有的話,我會返回下一個令牌。 但后來我想重做這個功能。
我就是這樣做的:
next_token = add_amazon_orders(xml_data=xml_data)
if next_token:
next_token = add_amazon_orders(xml_data=auth.list_orders(next_token=next_token['$']))
if next_token:
next_token = add_amazon_orders(xml_data=auth.list_orders(next_token=next_token['$']))
if next_token:
next_token = add_amazon_orders(xml_data=auth.list_orders(next_token=next_token['$']))
但是將來我可能會返回更多的下一個標記,我不想繼續添加更多的 if 功能。 這是丑陋和多余的。
我更喜歡這樣的東西:
next_token = add_amazon_orders(xml_data=xml_data)
while next_token:
next_token = add_amazon_orders(xml_data=auth.list_orders(next_token=next_token['$']).original)
但這不起作用,因為即使它的函數中有 next_token,while 循環也會停止。
實現這一目標的最佳方法是什么?
謝謝
抱歉,我在移動設備上,所以沒有格式化,但是:
while True:
if nextToken:
#Your code here
else:
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.