无法理解TypeError

时间:2011-08-29 00:43:14

标签: python

有人可以解释为什么我收到此错误?我的代码:

def x(n):
    if n == 1: n = 4
    elif n == 2: n = 3
    elif n == 3: n = 2
    elif n == 4: n = 1
    return n

def y(n):
    if n == 1: n = 2
    elif n == 2: n = 1
    elif n == 3: n = 4
    elif n == 4: n = 3
    return n

def query_x(i, j, quadrants):
    quadrants[i-1:j] = map(x, quadrants[i-1:j])

def query_y(i, j, quadrants):
    quadrants[i-1:j] = map(y, quadrants[i-1:j])

def query_c(i, j, quadrants):
    count = [quadrants[i-1:j].count(n) for n in range(1,5)]
    print "%d %d %d %d" % (count[0], count[1], count[2], count[3])

def process_queries(queries, quadrants):

    for query in queries:
        if   query[0] == "X": query_x(query[1], query[2], quadrants)
        elif query[0] == "Y": query_y(query[1], query[2], quadrants)
        elif query[0] == "C": query_c(query[1], query[2], quadrants)

if __name__ == "__main__":

    N = int(raw_input())

    quadrants = []
    for i in xrange(N):
        pair = map(int, raw_input().split())
        x, y = pair

        if   x > 0 and y > 0: quadrants.append(1)
        elif x < 0 and y > 0: quadrants.append(2)
        elif x < 0 and y < 0: quadrants.append(3)
        elif x > 0 and y < 0: quadrants.append(4)

    Q = int(raw_input())

    queries = []
    for i in xrange(Q):
        query = raw_input().split()
        queries.append([query[0], int(query[1]), int(query[2])])

    process_queries(queries, quadrants)

输出:

4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3
1 1 1 1
Traceback (most recent call last):
  File "QuadrantQueries_dev.py", line 53, in <module>
      process_queries(queries, quadrants)
  File "QuadrantQueries_dev.py", line 28, in process_queries
      if   query[0] == "X": query_x(query[1], query[2], quadrants)
  File "QuadrantQueries_dev.py", line 16, in query_x
      quadrants[i-1:j] = map(x, quadrants[i-1:j])
  TypeError: 'int' object is not callable

2 个答案:

答案 0 :(得分:3)

pair = map(int, raw_input().split())
x, y = pair

在第39行,您将x和y对象重新定义为整数。 x和y不再引用您在顶部定义的函数。你的&#34;主要&#34;代码块不是您仍在全局命名空间的单独函数范围。

http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces

答案 1 :(得分:0)

我认为您的代码可以使用一些改进。我决定改写整个事情。也许你会喜欢我的一些改变。

首先,我们可以使用字典来处理象限数字的重新映射。其次,我们可以创建辅助函数来计算象限数。然后我们可以构建象限数列表并对其进行处理。这段代码不是读取查询,构建它们的列表,然后再处理它们,而是只读取查询并处理它们。

dmapx = {1:4, 2:3, 3:2, 4:1}
dmapy = {1:2, 2:1, 3:4, 4:3}

def quadnum(x, y):
    if x >= 0 and y >= 0:
        return 1
    elif x < 0 and y >= 0:
        return 2
    elif x < 0 and y < 0:
        return 3
    else:
        return 4


def rewrite_x(quadrants, i, j):
    quadrants[i-1:j] = [dmapx[x] for x in quadrants[i-1:j]]

def rewrite_y(quadrants, i, j):
    quadrants[i-1:j] = [dmapy[y] for y in quadrants[i-1:j]]

def print_counts(quadrants, i, j):
    count = [quadrants[i-1:j].count(n) for n in range(1,5)]
    print "%d %d %d %d" % tuple(count)

def parse_quadrants():
    n = int(raw_input())
    for _ in xrange(n):
        yield [int(s) for s in raw_input().split()]

def parse_queries():
    n = int(raw_input())
    for _ in xrange(n):
        code, i, j = raw_input().split()
        i = int(i)
        j = int(j)
        yield (code, i, j)

if __name__ == "__main__":
    # parse quadrants from input, and build list of quadrant numbers
    quadrants = [quadnum(x, y) for x, y in parse_quadrants()]

    # parse and handle queries
    for code, i, j in parse_queries():
        if code == "X":
            rewrite_x(quadrants, i, j)
        elif code == "Y":
            rewrite_y(quadrants, i, j)
        elif code == "C":
            print_counts(quadrants, i, j)
        else
            raise ValueError, "bad code: %s" % str(code)