2
turns = [4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4]
turns.append([1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])
turns.append([4, 6, 2, 8, 1, 9, 6, 5, 3, 7, 6, 8, 2, 4]) 
turns.append([0, 0, 26, 24, 3, 7, 5, 4, 25, 25, 21, 21, 21, 21])
turns.append([0, 0, 0, 0, 7, 3, 8, 2, 0, 0, 29, 29, 29, 29])
turns.append([0, 0, 0, 0, 26, 24, 0, 0, 0, 0, 0, 0, 0, 0])

arrayValue = turns[j][i]
    if arrayValue == turnCurrently:


Error Message:
Traceback (most recent call last):
  File "D:/a45d32f947055690c690d94f88/TicTacToe", line 183, in <module>
    arrayValue = turns[j][i]
TypeError: 'int' object is not subscriptable

My question is essentially, I want to test if turns[j][i] is equal to turnCurrently but I keep running into this subscriptable problem, I haven't been able to find this answered anywhere I think that this is a very important question. Thanks in advance, Richard

1
  • 2
    Are you sure you want turns in this form? You create a plain list in the beginning, then append new lists to it, so it looks almost like 2D, but not quite. Commented Jun 4, 2012 at 11:00

4 Answers 4

6

Try this for building a two-dimensional array (a "matrix") of turns:

turns = [[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4]]
turns.append([1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])
turns.append([4, 6, 2, 8, 1, 9, 6, 5, 3, 7, 6, 8, 2, 4]) 
turns.append([0, 0, 26, 24, 3, 7, 5, 4, 25, 25, 21, 21, 21, 21])
turns.append([0, 0, 0, 0, 7, 3, 8, 2, 0, 0, 29, 29, 29, 29])
turns.append([0, 0, 0, 0, 26, 24, 0, 0, 0, 0, 0, 0, 0, 0])

Or simply this:

turns = [[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4],
         [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4],
         [4, 6, 2, 8, 1, 9, 6, 5, 3, 7, 6, 8, 2, 4],
         [0, 0, 26, 24, 3, 7, 5, 4, 25, 25, 21, 21, 21, 21],
         [0, 0, 0, 0, 7, 3, 8, 2, 0, 0, 29, 29, 29, 29],
         [0, 0, 0, 0, 26, 24, 0, 0, 0, 0, 0, 0, 0, 0]]

As you can see, a matrix is nothing more than a list of lists. You weren't building a matrix in the first place - the first row has to be a list too.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this really helped, it fixed the problem, I would have never thought of the beginning being ints and the later pars being lists. THANKS SO MUCH!!
@user1434957 if this answer was helpful for you, please accept it as correct (click the check mark to its left)
2

It often helps a lot to print out the content of your variables to see what it happening:

>>> turns
[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4, [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4], [0, 0, 26, 24, 3, 7, 5, 4, 25, 25, 21, 21, 21, 21], [0, 0, 0, 0, 7, 3, 8, 2, 0, 0, 29, 29, 29, 29], [0, 0, 0, 0, 26, 24, 0, 0, 0, 0, 0, 0, 0, 0]]

As you can see, the first few elements of that list are simple integers, but not a list of ints. So when j is small, it will select an int and try to apply [i] on it – which fails.

The solution in this case is to put the first elements into an extra list:

turns = [[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4]]
turns.append( … )

Another way would be to initialize your list as an empty list, and append the first sub-list as well:

turns = []
turns.append([4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4])
turns.append( … )

1 Comment

Really helpful too, that identified the problem... THANKS
2

You array have a strange layout. The first 14 elements are integers, then follows a couple of other arrays.

So what you basically have is this:

[4, 6, 2, ..., 2, 4, [1, 1, 2, ...], [4, 6, ...], ...]

You may solve this by changing the first assignment to:

turns = [[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4]]

Comments

1

change turns = [4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4] to

turns = [[4, 6, 2, 8, 1, 9, 5, 5, 3, 7, 6, 8, 2, 4]]

1 Comment

if you think the answer is useful give an upvote on my answer and accept it

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.