Wrong error


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/7?curriculum_id=4f89dab3d788890003000096#


Traceback (most recent call last):
File "python", line 5, in
TypeError: 'int' object is not callable


why does the error implies to line 5? i did this ages ago


from random import randint 

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

# Add your code below!
def random_row(board):
    from random import randint
    return randint(0, len(board)-1)
def random_col(board):    
    from random import randint
    return randint(0, len(board)-1)


#2

Unfortunately I am not able to reproduce this error.


You have to import randint only once, so it should be:

def random_row(board):
    return randint(0, len(board)-1)
def random_col(board):    
    return randint(0, len(board)-1)

#3

Hi @rkgames ,

Your message could be produced by first submitting code that assigned an int to the name, range, for example, as follows ...

range = 5

... and then by deleting that statement, and submitting the same code that you posted.

As a result, the console appears as follows ...

Traceback (most recent call last):
  File "python", line 5, in <module>
TypeError: 'int' object is not callable

The reason for the error is that the original line of code redefined the name, range, so that it no longer referred to the built in function, range. When your code attempted to call the function, it appeared to the Python interpreter that it was trying to call an int, as if it were a function.

To prevent this sort of problem, never use the name of a built in function or type as the name of a variable.

Also, when you remove names from your code on Codecademy, you should submit to make sure that your new code gets saved, then refresh the page, and submit again to see the result. The page refresh will purge Codecademy's memory of names that are no longer used in your code.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.