How can we read inputs as integers in Python?


In Python, there are two ways to provide input information to a program. One in which we directly assign input objects to object references/variables; and the other in which the program takes user input dynamically (during runtime).

Python 2.x versions use raw_input() function to take user input dynamically; whereas, Python 3.x versions use input() function. The input() function only takes input values in the form of strings, by default.

In this article, we will learn how to read the input as “Numbers” in Python 3.x using input() function.

Reading Inputs as Numbers

Python, like any other programming languages, accepts two types of numbers: integer type (int) and fractional type (float). Since the input() function only considers the inputs as strings, the only way to read them as any other type of data is by adopting the technique of typecasting.

Let us look at examples demonstrating the typecasting technique applied on the input() function.

Example

In this example, we are accepting the inputs as integers. Therefore, the input must be typecasted using int datatype.

integer = int(input("Enter an integer as input: ")) print(integer*10) print(type(integer))

Output

Let us compile and run the given program, to produce the result as follows −

Enter an integer as input: 12
120
<class 'int'>

Example

On contrary to the previous example, let us now try to accept the inputs as fractional numbers. Therefore, the input must be typecasted using float datatype.

num = float(input("Enter an number as input: ")) sq = num*num print(sq) print(type(num))

Output

Let us compile and run the given program, to produce the result as follows −

Enter an integer as input: 45.63
2082.0969000000005
<class 'float'>

Example

If the input is typecasted into float, integers can also be accepted. Let us see an example below.

num = float(input("Enter an number as input: ")) sq = num*num print(sq) print(type(num))

Output

Let us compile and run the given program, to produce the result as follows −

Enter an integer as input: 10
100.0
<class 'float'>

Example: Using Exceptions

Since the input is entered dynamically, there is chance the user might not enter a number. If that happens, we can consider it as an exception. To handle this exception, we are using a try/except block in while loop as shown the following example, which keeps executing until a number is accepted as an input.

user = 0 while True: try: user = int(input("Please enter a number: ")) except ValueError: print("Integer not found. Please enter again..") continue else: print("Integer found. Thank you.") break

Output

The output of the given program above, is produced as follows −

Please enter a number: a
Integer not found. Please enter again..
Please enter a number: 12
Integer found. Thank you.

Conclusion

Reading user input as numbers is a crucial part of making your code interactive and user friendly. While there are multiple ways to go about this, the most important thing is to make sure that your program is receiving the values that you want it to receive. That is why we have covered multiple methods that you can use for reading input as a number.

Updated on: 24-Feb-2023

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements