【问题标题】:Python--Function not returning valuePython--函数不返回值
【发布时间】:2017-04-07 08:04:20
【问题描述】:

我想写一个函数来比较两个值,val1和val2,如果val1大于val2,给a_points加1点(像A队一样),反之亦然(如果val2给b_points加一个点更大。) 如果这两个值是偶数,我不会向 a_points 或 b_points 添加任何点。

我的问题是 test_val 不会返回 a_points 或 b_points 的值。

a_points=0
b_points=0

def test_val(a_points,b_points,val1,val2):
    if val1 > val2:
        a_points+=1
        return a_points
    elif val2 > val1:
        b_points+=1
        return b_points
    elif val1==val2:
         pass

Here's a link to a visualization showing the problem.

【问题讨论】:

  • 如您的可视化所示,函数内部的a_pointsb_points 与全局变量不同。所以在函数内部修改它们不会修改全局的。您可能对global 关键字(又名添加global a_points, b_points)或return 关键字感兴趣。

标签: python python-3.x


【解决方案1】:

您的问题是 Python 整数是不可变的,这通常是值得阅读的。更多细节可以在here找到。

现在,关于解决方案:

  1. 按照建议,您可以使用global 变量。请记住,这通常被认为是不好的做法,因为它会导致代码混乱......但globals 在编程中占有一席之地。

  2. 同样建议,您始终可以同时返回 a_pointsb_points

  3. 使用lists 记分:

    • test_val 将返回 0、1 或 2,其中 0 表示相等,1 表示第一个参数较大,2 表示第二个参数较大。
    • 您的主脚本将有一个包含上述索引的列表,它将“保留分数”

代码:

a0=5
a1=6
a2=7
b0=3
b1=6
b2=10
points=[0, 0, 0]


def test_val(val1,val2):
    if val1 > val2:
        return 1
    elif val2 > val1:
        return 2
    elif val1==val2:
        return 0

points[test_val(a0,b0)] += 1
points[test_val(a1,b1)] += 1
points[test_val(a2,b2)] += 1

print("eq=%d, A=%d, B=%d" % (points[0], points[1], points[2]))

输出 (visualize)

eq=1, A=1, B=1

希望对你有帮助

【讨论】:

    【解决方案2】:

    全局变量通常是一个坏主意。除非真的必须,否则不要使用它们。

    实现这种计数器的正确方法是使用一个类。

    class MyCounter(object):
    
        def __init__(self):
            self.a_points = 0
            self.b_points = 0
    
        def test_val(self, val1, val2):
            if val1 > val2:
                self.a_points += 1
            elif val2 > val1:
                self.b_points += 1
            else:
                pass
    
    counter = MyCounter()
    counter.test_val(1, 2)
    counter.test_val(1, 3)
    counter.test_val(5, 3)
    print(counter.a_points, counter.b_points)
    

    输出:

    (1, 2)
    

    请注意,从test_val 返回一个值是没有意义的,因为调用者无法知道她得到的是a_points 还是b_points,所以她不能以任何有意义的方式使用返回值。

    【讨论】:

    • 这正是我想做的!我之前尝试过把它变成一个类,但是我失败了,然后上面提到的问题发生了,我觉得最好保持简单。非常感谢!
    • @KristenHuber 如果您觉得我的回答有帮助,您可以点赞。
    【解决方案3】:

    这将简化您的代码和逻辑。让它发挥作用;-)

    a0=5
    a1=6
    a2=7
    b0=3
    b1=6
    b2=10
    a_points=0
    b_points=0
    
    def test_val(a_points,b_points,val1,val2):
        if val1 > val2:
            a_points+=1
    
        elif val2 > val1:
            b_points+=1
    
        return a_points, b_points
    
    
    
    a_points, b_points = test_val(a_points,b_points,a0,b0)
    a_points, b_points = test_val(a_points,b_points,a1,b1)
    a_points, b_points = test_val(a_points,b_points,a2,b2)
    
    print(a_points,b_points)
    

    【讨论】:

      【解决方案4】:
      print (test_val(a_points,b_points,1,2))
      print (test_val(a_points,b_points,2,1))
      print (test_val(a_points,b_points,2,2))
      

      这会给你一个结果:

      1
      1
      None
      

      因此,您不应查看函数返回值,而是更新变量 a_points 和 b_points 的值。这就是为什么在您共享代码的链接中最后包含 print(a_points,b_points) 语句

      【讨论】:

        【解决方案5】:

        考虑一下:

        a0=5
        a1=6
        a2=7
        b0=3
        b1=6
        b2=10
        a_points=0
        b_points=0
        
        def test_val(a_points, b_points, val1, val2):
            if val1 > val2:
                a_points += 1
                return (a_points, b_points)
            elif val2 > val1:
                b_points += 1
                return (a_points, b_points)
            elif val1==val2:
                return (a_points, b_points)
        
        a_points, b_points = test_val(a_points,b_points, a0, b0)
        a_points, b_points = test_val(a_points,b_points, a1, b1)
        a_points, b_points = test_val(a_points,b_points, a2, b2)
        
        print(a_points, b_points)
        

        祝你好运!

        【讨论】:

        • 我认为按照塞巴斯蒂安的建议返回两个值是最好的解决方案。这样,函数始终具有相同的输出。我会在函数末尾只放一个return (a_points, b_points) 并删除elif 语句,但这些只是小改进(正如Gormador 刚刚发布的那样)
        • 非常容易理解。谢谢!
        【解决方案6】:
        a_points=0
        b_points=0
        
        def test_val(a_points,b_points,val1,val2):
            global a_points
            global b_points
        
            if val1 > val2:
                a_points+=1
                return a_points
            elif val2 > val1:
                b_points+=1
                return b_points
        
            elif val1==val2:
                 # If you pass, it won't return a_points nor b_points
                 return a_points # or b_points
        

        【讨论】:

          【解决方案7】:

          请注意,a_pointsb_points 会影响您的全局变量,因为它们也作为参数传递。

          无论如何,在相等的情况下你不是返回值,而是返回一个值而不是pass

          def test_val(a_points,b_points,val1,val2):
              if val1 > val2:
                  a_points+=1
                  return a_points
              elif val2 > val1:
                  b_points+=1
                  return b_points
              elif val1==val2:
                   return a_points
          

          【讨论】:

            猜你喜欢
            • 2014-08-22
            • 1970-01-01
            • 1970-01-01
            • 2019-02-12
            • 1970-01-01
            • 2015-07-03
            • 2016-04-18
            • 2018-03-13
            • 2020-12-04
            相关资源
            最近更新 更多