【问题标题】:How to return flag and result from a funcion (python)?如何从函数(python)返回标志和结果?
【发布时间】:2020-09-25 15:58:35
【问题描述】:

我们先看代码-

def findlinks():
    mylinks=[]
    if (condition):
        for x in range(int):
            if x==(int):
               #some operation goes here
            else:
                 #some operation goes here
                return mylinks,1        

    else:
        for x in range(int):
           if x==(int):
              #some operation goes here
                    return mylinks,0

           else:
                #some operation goes here
 links=[]    
 while (True):
        first,second=findlinks()
        links=links+first
        if second==1:
                 pass
        elif second ==0:
                  break

问题出在while loop。它永远不会运行多次。我认为这是因为当我从function 返回时,它会从functionloop 中退出。

任何替代方法来解决这个问题?还是对代码进行任何修改?

【问题讨论】:

  • @TomKarzes 它不会等待函数的结果。 While 循环已失效。
  • 哦,对不起,我看错了。那么while 循环将在second 等于0 时中断。
  • 如果您能提及代码的目标,我将不胜感激。您的代码更像是伪代码,很难确定目的。请记住,对您的函数的调用必须返回适当的 second 参数。

标签: python python-3.x function loops return


【解决方案1】:

很难说你到底想要达到什么目标。

如果你想在循环结束后返回,你可以这样做:

def func():
    list = []
    if condition:
        flag = 1
        # loop here, do something with the list
    else:
        flag = 0
        # loop here, do something with the list

    return list, flag

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多