How to make it only admit integer response?

2

Hello, I'm new to python and I wanted to ask you how to make a variable that only admits whole numbers in response to what I mean:

In C:

int numero;
printf("Ingrese un numero: ");
fflush(stdin);
scanf("%i",&numero);
printf("El numero ingresado es: %i", numero);

It is known that if you enter a string of characters or right away a decimal number within that variable that only allows whole numbers in C, there would be an error in the compilation, to what I am going, how do I achieve that in python? ? since in python from what I saw, no specific variables are declared.

If someone can write me that same code in python language I would be grateful and if possible the same example but with float and char. Thanks.

    
asked by Gerónimo Membiela 26.12.2018 в 06:11
source

2 answers

1

Python takes the type of a default value when you assign a result, if you enter 10, it will take it as an integer, if you enter "jose" as a string, if you enter 10.50, as float.

To declare that it is only integer, you must assign an integer:

  

Variable_A_Usar = int (input ("Enter integer"))

To see if your type was assigned correctly, use the "type" method to see its type.

    
answered by 26.12.2018 / 07:52
source
1

It is a good idea to capture the value entered and then validate if it is a number with isdigit (), thus avoiding errors in case the user has entered a non-numeric character.

     while True:
          variable_a_usar = input("Ingrese número entero: ")
          if variable_a_usar.isdigit() is True:
            variable_a_usar = int(variable_a_usar)
            break
        print("El número es %d" % (variable_a_usar))
    
answered by 26.12.2018 в 15:17