The objective is to verify if an integer was entered instead of a float, although when specifying if "x" is of type "int", what I want is to detect if a float was entered instead of an integer, so use " Try" so that the program does not fall in that case, but the problem is in requesting again that the integer value be entered in the same position that the float was entered in the array, since the program is not working correctly to be able to enter all the values, the for cycles of rows and columns do not finish going through properly, and so far I have not found the solution. My code is the following:
matriz=[]
x=int
bandera=True
filas=3
columnas=3
for i in range(filas):
for j in range(columnas):
while(bandera==True):
bandera=False
print("Introduzca valores Enteros para la matriz")
print("fila:",i)
print("columna:",j)
try:
x=int(input("Valor entero: "))
except ValueError:
bandera=True
print("Se ingreso un valor real, se solicito un entero")
matriz[i][j]=x
Is it okay to declare like this: x=int?
There must be many ways to check for an integer, but your code has errors that have nothing to do with integer verification, one of the errors I see is that at the beginning you define the array as a one-dimensional list:
But when defining to fill the array of values you make reference to multidimensional indices:
This will give an error saying the index is out of range. An easy way to solve this is to create an empty array before filling it with values of the dimensions you specify in the row and column variables:
And finally, to verify if it is an integer or a float, there must be many ways, but it occurs to me to use the method of the variable type
float
that returns true if the float is an integer calledis_integer()
, here is an example so you can see how it works:As this method is exclusive for
float
then you must cast what you receive in the input to be able to use it (in case the user types aint
), like this:With my suggestions the code would look something like this:
I don't know if it's the most efficient way to do it, but it works, good luck with your programming!