How to take integer input in Python?

In this post, We will perceive how to take whole number contribution to Python. As we realize that Python worked in input() work consistently returns a str(string) class object. So for taking whole number info we need to type cast those contributions to whole numbers by utilizing Python worked in int() work.

Consideration! Reinforce your establishments with the Python Programming Foundation Course and gain proficiency with the essentials. In any case, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. Furthermore regardless your Machine Learning Journey, join the Machine Learning – Basic Level Course.

There is contrast by they way you can take input in python 2.x and 3.x. You want to utilize raw_input in python 2.x and contribution to Python 3.x In Python 3.x, raw_input was renamed to include and the Python 2.x information was taken out. As you would know, raw_input consistently returns a String article and same is the situation with input in Python 3.x To fix the issue, you want to unequivocally make those contributions to numbers by placing them in int() work.

Instructions to Take Integer Input From Command Line In Python

In this scratch pad, we will see how to take Integer input from order line in Python 3 and Python 2. For taking string input from order line in Python, look at How To Take String Input From Command Line In Python.

Integer Input From Command Line In Python 2

Python raw_input() allows taking input from the command line, but by default, all the inputs are treated as strings.

In [1]:
userinput = raw_input("Enter Integer Number!\n")
print("You entered %d"%userinput)
Enter Integer Number!
5
————————————————————————–
TypeError                                 Traceback (most recent call last)
<ipython-input-1-6e2b3ab536e9> in <module>()
      1 userinput = raw_input("Enter Integer Number!\n")
----> 2 print("You entered %d"%userinput)
TypeError: %d format: a number is required, not str

The reason we got above error is “userinput” variable contains a string but not a number.

To fix this, we will have to convert the input to integer before assigning to a variable.

In [2]:
userinput = int(raw_input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
Enter Integer Number!
5
You entered 5

Float Input From Command Line In Python 2

Similarly we can tweak our previous code to take a Floating point number as input.

In [3]:
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
Enter Floating Point Number!
5.6
You entered 5.600000

Integer Input From Command Line In Python 3

Similarly we can use above code snippets in Python 3 by replacing the Python input function raw_input() with input().

In [4]:
userinput = int(input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
Enter Integer Number!
5
You entered 5

Float Input From Command Line In Python 3

In [5]:
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
Enter Floating Point Number!
5.6
You entered 5.600000

Example 1:

# take input from user
input_a = input()
 
# print data type
print(type(input_a))
 
# type cast into integer
input_a = int(input_a)
 
# print data type
print(type(input_a))

Output:

100
<class 'str'>
<class 'int'>

Example 2:

# string input
input_a = input()
 
# print type
print(type(input_a))
 
# integer input
input_b = int(input())
 
# print type
print(type(input_b))

Output:

10
<class 'str'>
20
<class 'int'>

Example 3:

# take multiple inputs in array
input_str_array = input().split()
 
print("array:", input_str_array)
 
# take multiple inputs in array
input_int_array = [ int(x) for x in input().split()]
 
print("array:", input_int_array)

Output:

10 20 30 40 50 60 70
array: ['10', '20', '30', '40', '50', '60', '70']
10 20 30 40 50 60 70
array: [10, 20, 30, 40, 50, 60, 70]

Also Read: How to design a C++ project for beautiful animated scenery?

Leave a Reply

Your email address will not be published. Required fields are marked *