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.
Table of Contents
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.
userinput = raw_input("Enter Integer Number!\n")
print("You entered %d"%userinput)
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.
userinput = int(raw_input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
Float Input From Command Line In Python 2
Similarly we can tweak our previous code to take a Floating point number as input.
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
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().
userinput = int(input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
Float Input From Command Line In Python 3
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
Example 1:
- Python3
# 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:
- Python3
# 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:
- Python
# 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?