NoahSonic123 NoahSonic123 - 2 months ago 79
Python Question

TypeError: 'int' object is not subscriptable?

So, I was just happily coding along, and ran into this error:

Traceback (most recent call last):
File "DungeonGame.py", line 92, in <module>
possible = possibleMoves(locations["player"])
File "DungeonGame.py", line 65, in possibleMoves
if player[0][0] == 0:
TypeError: 'int' object is not subscriptable


Now, I have no clue what this means, but hopefully you do. Here's all of the code involved.

#Function Error happens in:
def possibleMoves(player):
options = ["RIGHT", "LEFT", "UP", "DOWN"]
if player[0][0] == 0:
options.remove("LEFT")
elif player[0][0] == 4:
options.remove("RIGHT")
elif player[0][1] == 0:
options.remove("DOWN")
elif player[0][1] == 4:
options.remove("UP")
return options
#Line that calls function:
possible = possibleMoves(locations["player"])
#dict that player is in: (this is an example, the tuples after could be anything like (0-4, 0-4))
locations = {"monster" : (1, 2), "door" : (3, 2), "player" : (4, 1)}


One thing to note is that the function only breaks on 2nd time it runs, it works fine the first time. The only change between then is in the input, where player's value changes, but it is still a tuple with 2 ints.

Answer Source

Here is the reason why you get the error:

player[0][0] == (4, 1)[0][0] == 4[0]

You pass locations["player"] to possibleMoves(player), so player == locations["player"] == (4, 1).