格式化Python代码后,出现错误

时间:2018-10-01 00:22:59

标签: python

我编写了一个python代码,该代码可以正常工作且没有错误:

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


    # check if the partial sum is equals to target
    if s%3 == 0:
       # print "sum(%s)=%s" % (partial, target)
        if s != 0:
            str1 = ''.join(str(e) for e in partial)
            y = int(str1)
            str1 = ''.join(str(e) for e in max)
            z = int(str1)
            if y>z:
                max = partial

    if s >= 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, 0, 0, 0,0];

    subset_sum([3,1,4,1,5,9],15)
    max =  sorted(max, key=int, reverse=True)
    print max
    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15

我稍微修改了代码:

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


    # check if the partial sum is equals to target
    if s%3 == 0:
       # print "sum(%s)=%s" % (partial, target)
        if s != 0:
            str1 = ''.join(str(e) for e in partial)
            y = int(str1)
            str1 = ''.join(str(e) for e in max)
            z = int(str1)
            if y>z:
                max = partial

    if s >= 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])

  def answer(l):

    max = [0, 0, 0, 0,0];
    subset_sum(l,15)
    max =  sorted(max, key=int, reverse=True)
    return max

  answer([3,1,4,1,5,9])

基本上,我只是添加了另一个函数,该函数调用subset_sum。 但是,新格式的代码出现错误:

  hi
  Traceback (most recent call last):
  hi
    File "C:/Users/Bob/PycharmProjects/test/test.py", line 33, in <module>
      answer([3,1,4,1,5,9])
    File "C:/Users/Bob/PycharmProjects/test/test.py", line 29, in answer
      subset_sum(l,15)
    File "C:/Users/Bob/PycharmProjects/test/test.py", line 24, in subset_sum
      subset_sum(remaining, target, partial + [n])
    File "C:/Users/Bob/PycharmProjects/test/test.py", line 13, in subset_sum
      str1 = ''.join(str(e) for e in max)
  TypeError: 'builtin_function_or_method' object is not iterable

我的格式化代码有什么问题?只是为了阐明我的原始代码是完美的,我的原始代码是第一个代码片段。然后,第二个代码段是格式化的代码,但是这给了我错误。我显示了我的错误。我不知道为什么会出错,请帮忙。

-更新的代码-

  maximum = [0, 0, 0, 0, 0]
  def subset_sum(numbers, target, partial=[]):

    global maximum
    s = sum(partial)


    # check if the partial sum is equals to target
    if s%3 == 0:
       # print "sum(%s)=%s" % (partial, target)
        if s != 0:
            str1 = ''.join(str(e) for e in partial)
            y = int(str1)
            str1 = ''.join(str(e) for e in maximum)
            z = int(str1)
            if y>z:
                maximum = partial
                print maximum

    if s >= 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])

  def answer(l):
    maximum = [0, 0, 0, 0, 0]
    subset_sum(l,15)
    maximum =  sorted(maximum, key=int, reverse=True)
    return maximum

  print(answer([3,1,4,1,5,9]))

1 个答案:

答案 0 :(得分:0)

首先,请注意:不要使用max作为变量名;它是一个内置函数,可返回列表中具有最高值的项目。

subset_sum()函数中,您有一行

str1 = ''.join(str(e) for e in max)

这会尝试迭代(循环)预定义的函数,因为在使用max之前没有重新定义(使用变量之前,必须先创建/定义/初始化变量)。

当然,您在函数的开头确实有global max,但是由于在函数外部没有名为max的变量,因此默认情况下使用内置函数。


固定代码(请注意_max而不是maximum的用法):

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

    global _max
    s = sum(partial)


    # check if the partial sum is equals to target
    if s%3 == 0:
       # print "sum(%s)=%s" % (partial, target)
        if s != 0:
            str1 = ''.join(str(e) for e in partial)
            y = int(str1)
            str1 = ''.join(str(e) for e in _max)
            z = int(str1)
            if y>z:
                _max = partial

    if s >= 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])

def answer(l):
    global _max
    subset_sum(l,15)
    _max =  sorted(_max, key=int, reverse=True)
    return _max

_max = [0, 0, 0, 0,0];
answer([3,1,4,1,5,9])