Python input function to Get User Input – This Python tutorial will explain the use of Python input() function to get the input from a user during execution.
Getting input frrom the User
The input function is a built-in function in Python. It is a simple way for your program to get input the users at run time.
The Syntax / General form of using the input() function
The basic general form of input() function is written as an assignment statement:
variable_name = input( ‘message to user’)
Where input() function is used on the right hand side. Here variable_name is a valid name of a variable and ‘message to user ‘ is a string enclosed in single quotes or double quotes. We will prefer to use single quotes here.
Example of Using the Python input() function
fname = input( ‘What is your first name, please: ‘ )
This statement will print the given message ‘What is your first name, please: ‘ on the screen. The user will enter his name through keyboard and press Enter. The entered name will be stored in the variable name ‘fname’ on left side of asssignment operator ‘=’.
if we use a second statement after input statement as follows:
print( ‘Hello, ‘ , fname,’, Have a good day!’)
Now the second statement which is print() statement will be executed and prints a hello message on the screen. It prints the strings in single quotes as it is. Moreover, it will print the value of the variable fname which is the name entered by the current user.
How to Get String Input by Python input() Function
We just write an assignment statement with a variable name and an input() function, as follows:
fname = input(‘Enter your nam:’)
Therefore, the use will view the message and enter her name. The name is stored in the variable ‘fname’ on the left side.
How to get Numeric Input by Python input() Function
How to get an Integer input – Whole Numbers
To get an integer number or floating point number input we will use the input() function with eval() function.
whole_number = eval( input(‘Enter first number:’) )
How input() function Works with eval() function
First of all input() function will execute and show a message on screen. The user will enter a number for exammple ‘100’. Note that this ‘100’ is a string. Therefore, eval() function will take ths numeric string and evaluate its numeric value 100. This 100 is stored in the variable ‘whole_number’.
Another way to convert numeric string to a whole number
We can use int() function to convert a numeric string to an integer that is whole number.
num1 = int(input(‘Enter first number:’))
How to get a floating point number in Python Program
To get a floating point number, we will use the same general form of the inut() function as mentioned above.
radius = eval(input(‘Enter radius of circle:’))
Another way to get a float number is using float() function to convert numeric string to float value.
radius = float(input(‘Enter radius of circle:’))
Using the Python input() function in programs
fname = input( 'What is your first name, please: ' ) print( 'Hello, ' , fname,', Have a good day!') num1 = eval(input('Enter a number to display square:')) print('Square of this number=',num1*num1) fnum = eval(input('Enter a floating point number:')) print('You enterd:',fnum) n1=int(input('Enter a whole number=')) print('Yoou enterd:',n1) n2=float(input('Enter a real number with decimal point:')) print('You entered:',n2)
The output is:
What is your first name, please: Mahmood
Hello, Mahmood , Have a good day!
Enter a number to display square:5
Square of this number= 25
Enter a floating point number:12.56
You enterd: 12.56
Enter a whole number=100
Yoou enterd: 100
Enter a real number with decimal point:3.1415
You entered: 3.1415
Common Errors While Using input() function
When we use input() function to get numeric input, we must supply numeric string, otherwise the Python interpretor will show an error message. Since int() cannot convert a non-numeric string to integer. For example:
>>> num = int(input('Enter a number:')) Enter a number:hello Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> num = int(input('Enter a number:')) ValueError: invalid literal for int() with base 10: 'hello' >>>
Pingback: Python Multiplication Table of Number by Function Program | EasyCodeBook.com