It won't let me execute the function, does anyone know what's going on? How can I solve it? It's a chess game in which I try to make the moves and without valid king or not, but it keeps throwing me these errors, can someone help me?
def legal_move(before_move, last_move, move_min_max):
jugada = [0,0]
while jugada == [0,0]:
if before_move[0] - last_move[0] > move_min_max[0]:
print("this move is not valid")
jugada[0] = 0
else:
columna = before_move[0]
jugada[0] = 1
if before_move[1] - last_move[1] > move_min_max[1]:
print("this move is not correct")
jugada[1] = 0
else:
fila = before_move[1]
jugada[1] = 1
nueva_posicion = [columna, fila]
return nueva_posicion
columna = int(input("cual es la columna"))
fila = int(input("cual es la fila"))
posicion_nueva=[columna,fila]
posicion = [1,1]
legal_move(posicion_nueva,posicion, move_min_max=1)
And I have a problem, it returns TypeError: 'int' object is not subscriptable
The error
TypeError: 'int' object is not subscriptable
when trying to access an element or subscript of an object, but... what does it mean if an object is subscriptable or not. The term subscriptable is used to say that an object (remember that in python everything is an object) stores other objects, such as:""
()
[]
{key:value}
And that in turn its indices can be accessed using brackets
[]
, in the case ofset
it does not meet such a condition, even though it stores other objects its indices cannot be accessed withset[index]
, for this reason, a subscriptable object is when it contains other elements and in turn its elements can be accessed.The error practically says that you are treating an integer data type as a subscriptable object (the ones already mentioned), which could be translated to:
Where it is about accessing the position
0
of a number which is not correct.Well, what could you do in these cases? The answer is easy, check your variables either the ones you're sending to a function or the ones you're trying to access with the notation
object[index]
, but if you really want to have an object that you can access at its indices, you can choose to cast the object using the different functions that exist. What:str(object)
: Convert to a string.tuple(object)
: convert to a tuple.list(object)
: Convert to a list.dict(object)
: Convert to a dictionary.object
is the object (or variable) you want to convert, just remember to store the result in a variable, such asnuevo = str(viejo)
and use the new variable instead.Another way can be with the function
range()
, it only works with numbers, which generates a range of numbers and you can also access its indices.range(int)
: generate a range of numbers from 0 to n-1