0

I'm new to Python and I'm trying to create a simple tictactoe game to learn the syntax and keep getting an error TypeError: object of type 'int' has no len() when running the following:

board = [ 
            ['|', '|', '|' ],
            ['|', '|', '|'],
            ['|', '|', '|'] 
        ] 

def tictactoe ():

    print('\n'.join([''.join(['{:4}'.format(item) for item in row]) for row in board]))

    print(len(board))

    for i in range(0,len(board)):
        for j in range(0,len(i)):
            print(i,j)

3 Answers 3

2

The error is coming from the second line of your for loop:

for j in range(0, len(i))

This won't work since you're trying to get the length of i which is an integer value here. Instead, you can just go through the range of i as:

for j in range(0, i)
Sign up to request clarification or add additional context in comments.

Comments

0

int has no len() implies you are calling len on an int. Scanning your code shows len(i) is the only place len is called. And indeed, if you use for i in range(...) then i will be an int.

Perhaps you meant to use

for i in range(0,len(board)):
    for j in range(0, i):
        print(i,j)

instead.

Comments

0
for i in range(0,len(board)):
    for j in range(0,len(i)):
        print(i,j)

You declared i as an int in the first line here. just use

for j in range(0, i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.