在Python中修改和访问类变量

时间:2018-09-30 22:20:34

标签: python

我的python代码中有一个使用递归的方法。本质上,我需要一个变量,该变量在每次递归时都不会被擦除。我需要方法外的类变量,方法可以在其中使用和修改。到目前为止,这是我的代码:

def subset_sum(numbers, target, partial=[]):
    s = sum(partial)

    # check if the partial sum is equals to target
    if s %3 == 0 and s!=0:
        print "sum(%s)=%s" % (partial, target)
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n])


if __name__ == "__main__":
    max = 0;

    subset_sum([10,9,8,7,6,5,8,9,7],10000000)
    print max
    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15

我试图做这样的事情来使类变量:

  max = 0;
  def subset_sum(numbers, target, partial=[]):
    s = sum(partial)


    # check if the partial sum is equals to target
    if s %3 == 0 and s!=0:
        print "sum(%s)=%s" % (partial, target)
        if max<s:
            max = s
            print max
            print s
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n])


  if __name__ == "__main__":
    max = 0;

    subset_sum([10,9,8,7,6,5,8,9,7],10000000)
    print max
    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15

我说max = s,但我遇到了大量错误:

sum([10, 9, 8])=10000000
Traceback (most recent call last):
  File "C:/Users/BOB/PycharmProjects/test/test.py", line 24, in <module>
    subset_sum([10,9,8,7,6,5,8,9,7],10000000)
  File "C:/Users/BOB/PycharmProjects/test/test.py", line 18, in subset_sum
    subset_sum(remaining, target, partial + [n])
  File "C:/Users/BOB/PycharmProjects/test/test.py", line 18, in subset_sum
    subset_sum(remaining, target, partial + [n])
  File "C:/Users/BOB/PycharmProjects/test/test.py", line 18, in subset_sum
    subset_sum(remaining, target, partial + [n])
  File "C:/Users/BOB/PycharmProjects/test/test.py", line 9, in subset_sum
    if max<s:
UnboundLocalError: local variable 'max' referenced before assignment

我在做什么错了?

-更新

  global max
  def subset_sum(numbers, target, partial=[]):
    s = sum(partial)


    # check if the partial sum is equals to target
    if s %3 == 0 and s!=0:
        print "sum(%s)=%s" % (partial, target)
        if max<s:
            max = s
            print "hi"
        print max
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n])


  if __name__ == "__main__":
    max = 0;

    subset_sum([10,9,8,7,6,5,8,9,7],10000000)
    print max
    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15

该错误与以前的错误相同:

  File "C:/Users/BOB/PycharmProjects/test/test.py", line 9, in subset_sum
    if max<s:
  UnboundLocalError: local variable 'max' referenced before assignment

0 个答案:

没有答案