Loek Janssen
Loek Janssen

Reputation: 451

'tuple' not callable error

I now that this question has been asked several times. However, the answers do not seem to resolve my problem. I get a type error, 'tuple' object is not callable. I get this even though the tuple inside the list is separated by commas in the correct way:

def aiMove(b):
    movesList = moves(b, -1)
    heuristic = []
    if movesList:
        for m in movesList:
            bt = copy.deepcopy(b)
            print("bt: ", bt)
            bt[m[0]][m[1]] = -1         
            h = heat[m[0]][m[1]]
            for move in m[2]:
                i=1;
                try:
                    while (-1* bt[m[0] + i*move[0]][m[1] + i*move[1]] < 0):
                        bt[m[0] + i*move[0]][m[1] + i*move[1]] *= -1    
                        bt[m[0] + i*move[0]][m[1] + i*move[1]] += -1    
                        i += 1;
                except IndexError:
                    continue

            alpha = max(float('-inf'), alphabeta(bt, depth-1, h, float('-inf'), float('inf'), 1))
            heuristic.append(alpha)
            if (float('inf') <= alpha):
                break
            selectedMove = movesList[int(heuristic.index(max(heuristic)))]
            move(b, selectedMove, -1)
    else:
        print ("The AI can't move!")
            return []

selectedMove is (3, 2, [(0 , 1)]) for example. The moves() function returns a list of moves like the final value for selectedMove. It is an alpha beta pruning implementation for game tree search. The move() function actually moves the piece to that position and updates the board to finish the computers' turn. the variable b represents values on the current game board Every conversion I try (i.e. force selectedMove to be a list in itself, etc..) will give the same error in the line "move(b, selectedMove, -1)"

Does somebody see what may be wrong?

Microsoft Windows [Version 6.1.7600]  
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
    return self.func(*args)
  File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 131, in eventfun
    placePiece(x, y, b, n)
  File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 119, in placePiece
        project2.aiMove(b)
  File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\project2.py", line 225, in aiMove
    move(b, selectedMove, -1)
TypeError: 'tuple' object is not callable

Upvotes: 3

Views: 32858

Answers (1)

DSM
DSM

Reputation: 353559

In this line, you say that you're going to use the name move to refer to elements of m[2]:

for move in m[2]:

But then later, you try to call a function that you've called move:

move(b, selectedMove, -1)

Once you've seen this, the error message makes complete sense:

TypeError: 'tuple' object is not callable

because the name move doesn't refer to the function any more, but to the last tuple it was bound to in the loop.

More important than fixing this particular error (don't use move for a variable name if you also want to use it to refer to a function) is recognizing how the interpreter told you exactly what the problem was.

It said that a tuple wasn't callable; in the line of code it complained about, you were calling move; thus move was a tuple, and your next step should've been adding print(move) (or print(type(move), repr(move)) if you want to be fancy) to see what tuple it actually was.

Upvotes: 9

Related Questions