【问题标题】:Python : How to return two values from function which is called from elif?Python:如何从 elif 调用的函数返回两个值?
【发布时间】:2021-04-13 21:26:45
【问题描述】:

这里的elif someother_function():检查是否为真,还需要打印返回值。 谁能帮我解决这个问题?

def some_function():
     if correct:
         return True
     else:
         return False

def someother_function():
     if correct:
         return True,f
     else:
         return False,None


if some_function():
     print("something")
elif someother_function():
     print(f)
else:
     print("nothing")

这里,如何将 f 从 someother_function 返回到它在 elif 调用的地方?

【问题讨论】:

    标签: python-3.x function if-statement


    【解决方案1】:

    你可以试试:

    def some_function(correct):
       if correct:
           return False
       else:
           return False
    
    def someother_function():
        if correct:
           return True, f
    else:
        return False, None
    
    if some_function():
        print("something")
    elif [x := someother_function()] and x[0]:
        print(x[1])
    else:
        print("nothing")
    

    【讨论】:

    • 是的,这有帮助。但是在我原来的程序中检查 some_function() 之前我不能调用 someother_function。
    • 我在寻找类似 @​​987654322@ 的东西。
    • @ImageSchnell 或者你可以试试x := someother_function(True) 请看看我上次编辑的内容
    • 没见过这种编码,我试试。
    • 对不起,我不明白你做了什么。
    【解决方案2】:

    也许不是你想要的,但你可以把你的函数变成生成器。

    def some_function():
        return correct
    
    def someother_function():
         if correct:
             yield True
             yield "A"
         else:
             yield False
             yield None
    someother_function_gen = someother_function()
    
    
    if some_function():
         print("something")
    elif next(someother_function_gen):
         print(next(someother_function_gen))
    else:
         print("nothing")
    

    【讨论】:

    • 在检查if some_function()之前不能调用someother_function_gen = someother_function()
    【解决方案3】:

    我认为您需要指定一种事件。如果需要,请检查此代码并尝试:

    import random
    
    event = random.choice([True, False])
    
    def some_function(correct):
         if correct:
             return True
         else:
             return False
    
    def someother_function(correct):
         if correct:
             return True, f
         else:
             return False, None
    
    
    if event == True:
        some_function()
        print("something")
    elif event == False:
        someother_function():
        print(f)
    else:
        print("nothing")
    

    【讨论】:

      【解决方案4】:

      如果你愿意,你可以用你的函数编写一个类,并用你的输出设置一个类变量:

      import random
      
      class foo:
          
          def __init__(self):
              self.f=None
              
          def some_function(self):
              correct= random.choice([True, False])
              if correct:
                  return True
              else:
                  return False
      
          def someother_function(self):
              self.f = "something 2"
              correct= random.choice([True, False])
              if correct:
                  return True,
              else:
                  return False,None
      
      if __name__=="__main__":
          temp = foo()
          if temp.some_function():
               print("something")
          elif temp.someother_function():
               print(temp.f)
          else:
               print("nothing")
      

      【讨论】:

      • 谢谢,@Mazziotti,我一直在寻找更简单的方法,我想我必须让我的班级参与其中。我猜没有办法直接将两个值返回给 elif。
      • 也许解决方案比我们想象的要简单
      猜你喜欢
      • 2012-04-02
      • 2016-05-07
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多