In Python, one can use the input() method to receive the user input. For example, if you want the user to enter two values x, y, and then find the sum of those values, you can do it as follows :
x=input("Enter the value of x: ") y=input("Enter the value of y: ") print("x+y is: ",x+y)
Output :
Enter the value of x: 5 Enter the value of y: 6 x+y is 56
The sum of two numbers 5 and 6 should be 11. However, the output is 56.
If you’re wondering why? Then it is because the input() function returns a string object. Refer to the below code snippet for more details.
x=input("Enter the value of x: ") y=input("Enter the value of y: ") print(type(x)) print(type(y))
Output:
Enter the value of x: 5 Enter the value of y: 6 <class 'str'> <class 'str'>
x+y returns the concatenation of the strings “5” and “6” which is “56”.
If you want to perform some calculation on the numerical values input from the user, read along. In this article, we’ve covered different ways to read inputs as numbers.
Table of Contents
Solution 1: Converting the input Integer
To read the user inputs as integers, convert the input string object to an integer object using the int() function.
int(input("Message:")
In our sample program, we can convert the x and y to integer objects as shown below.
x=int(input("Enter the value of x: ")) y=int(input("Enter the value of y: ")) print(" x+y is ",x+y)
Output:
Enter the value of x: 5 Enter the value of x: 6 x+y is 11
As seen in the above output, the string objects returned from the input() function are converted into integers to perform the calculations.
Solution 2: Converting the input to float
If you’re expecting a float type value, then you can convert the string object to a floating-point object using the float() function. To do so, use the below command.
float(input("Message:")
In our sample program, we can convert the x and y to floating-point objects as shown below.
x=float(input("Enter the value of x: ")) y=float(input("Enter the value of y: ")) print("x+y is ",x+y)
Output:
Enter the value of x: 3.4 Enter the value of y: 4.4 x+y is 7.800000000000001
If you see the ValueError: could not convert string to float, check the link to know how to fix it.
Solution 3: Using pyinputplus package
There is a package named pyinputplus which provides functions to read the inputs as integers. To use this function in your program, you have to install it from PyPI and then use it.
1. To install pyinputplus to your system, open your terminal and run the below command.
pip install pyinputplus
2. Import the package in your program using the below statement.
import pyinputplus
Method 1: Read input as integers using inputInt()
To read the input as integers, make use of the inputInt() function as shown below in the below example.
import pyinputplus x=pyinputplus.inputNum("Enter the value of x: ") y=pyinputplus.inputNum("Enter the value of y: ") print("x+y is ",x+y)
Output:
Enter the value of x: 5 Enter the value of y: 6 x+y is 11
Method 2: Read input as floating-point numbers using inputFloat()
If you want to read the input as a floating type integer, use the inputFloat() function as shown below.
import pyinputplus x=pyinputplus.inputFloat("Enter the value of x: ") y=pyinputplus.inputFloat("Enter the value of y: ") print("x+y is ",x+y)
Output:
Enter the value of x: 4.5 Enter the value of y: 3.5 x+y is 8.0
Method 3: Read input as numbers( integers, floating-point numbers) using InputNum()
If you’re expecting floating-point numbers as well as integers in your gram, you can make use of the inputNum() function as shown below.
import pyinputplus x=pyinputplus.inputNum("Enter the value of x: ") print(type(x))
Output 1: Check for Integers
Enter the value of x: 23 <class 'int'>
Output 2: Check for Floating-point numbers
Enter the value of x: 5.5 <class 'float'>
Now, let’s try implementing this in our example program.
import pyinputplus x=pyinputplus.inputNum("Enter the value of x: ") y=pyinputplus.inputNum("Enter the value of y: ") print("x+y is ",x+y)
Output:
Enter the value of x: 5 Enter the value of y: 6 x+y is 11
Conclusion
In this short tutorial we, have discussed various options available to read the input as numbers in Python. We hope this has been informative. Kindly comment and let us know if this helped you. Thanks for reading.
1 thought on “How to read inputs as numbers in Python?”