Simple input and output
In python, to get the input from the user interactively, you can
use built in function input( )
The function input( ) is used in the following manner:
Variable_to_hold_the_value= input(<prompt to be display>)
For example,
name=input(‘What is Your Name’)
the above statement will display the
prompt as:
in[3]: name = input(“What is your name”)
what is your name -type input data here
in which you can type the name. The value that you type
in front of the displayed prompt will be assigned to given variable, name
reading numbers
string values cannot be used for arithmetic or other numeric
operators. For these operations, you need to have values of numeric
types(integer or float).
But what you would you do if you have to read numbers (integer or
float) ? The function input( ) returns the entered in string type only.
Python offers two functions int( ) or float( ) to be used with
input( ) to convert the values received through input( ) into int and float type.
You can :
1 – read in the value using the input( ) function.
2 – and then use int( ) or float( ) function with the read value
to change the type of input value to int or float respectively.
If you are planning to input an integer of floating – point
numbers using input( ) inside int( ) or float( ) such as shown below –
age = int(input(“Enter Your Age”))
or
per=float(input(“Enter your percentage”))
then you must ensure that the value you are entering must be in a
form that is easily convertible to the target type.
1 - In other words while inputting integer values using int( )
with input( ), make sure that the value being entered must be int type
compatible.
2 – while inputting floating point values using float( ) with
input( ), make sure that the value being entered must be float type compatible.
SIMPLE INPUT AND OUTPUT
Reviewed by Learn Python
on
November 03, 2019
Rating:
No comments: