Taking Input from user in Python

05 min read

We use the command ‘input ()’ take user-defined inputs into the program. All the inputs from the user are taken as variables of type <class ‘str’ > by default. Hence, if int are taken as input its mandatory to change their class in order to use them as integers for further calculations.

For example,

Name= input(‘ What is your name?’)

print(‘welcome’,Name)

 Input: XXX

Output: welcome XXX

#Note that the space between welcome and XXX is produced due to the presence of ‘,’(comma) in the print statement.

 Now, let us consider an example program:

 Challenging task: European lifts in an apartment is generally marked from zero in the ground floor and later increment 1 on each floor whereas, in the USA, the ground floor is generally marked by 1 and later incremented by 1 to each floor. Write a program to convert an EU floor number to USA floor number.

 

floor = input(‘Enter EU floor’)
US_floor=int(floor) + 1                        #Note: All inputs taken are always stored as strings by default.
print(‘US floor = ‘, US_floor)              #Therefore, conversion to int/float for calculations is mandatory.

Output:
Enter EU floor 3
US floor =4

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error