Python: More on input( ) function

input() Function

The purpose of input() function is to read input from the standard input (the keyboard, by default). Using this function, you can input the string values not numbers. The general format is:

input([prompt])

Here,

  • The prompt is an optional parameter and where prompt is the string you wish to display on the screen.
  • The prompt is an expression that serves to prompt the user for input. This is almost always a string literal which is enclosed within quotes (single or double) with parentheses.
  • The result of input() function is displays a value in string on the screen.

For example,

num = input(“Enter any number. “)

celsius = input(‘Enter the Celsius temperature: ‘)

If you see carefully, you would probably notice the blank space inside the quotes at the end of these prompts. We usually put a space at the end of a prompt so that the input that the user types do not start right next to the prompt. Putting a space makes the interaction easier to read and understand.

If you simply type num = input(), then you may get confused to input data through keyboard. Without the prompt, the user would not know what is happening, and the computer would just sit there waiting! For more clarity, it is important to utilize the prompt. Make sure that the input data is valid for the respective variable.

For example,

  • In Python Shell window click Ctrl+O to create a new window for script programming.
  • Type the following lines:

 NOTE: The purpose of an input function is to get some information from the user of a program and store it in to variable.

# input() function demonstration.

rollno = input(“Enter your roll no. “)

marks = input(‘Enter your total marks: ‘)

print (‘My roll no. is:’, rollno)

print (‘My name is:’, marks)

  • Save the file as: input.py.
  • Click Run -> Run Module or Press F5. Notice that it will ask for data for roll number and your name in Shell window as shown below:

>>>

Enter your roll no. 100 Enter your total marks: 345        (Press Enter key after every input)

My roll no. is: 100

My name is: 345

>>>

Notice that when you entered something, i.e., the program has taken as the input data and stored irrespective variables. This is exactly the input() function does. First it prints the string you give as a parameter (in this case ‘Enter your roll no. ‘),.and then it waits for a line to be typed in, and returns the integer value you typed. In the input.py program this value is assigned to the variables rollno and marks.

Inputting Numeric Values

The input() function reads strings of characters from the standard input device like keyboard. These data cannot be processed as any mathematical operation like +, -, *, /, etc. For example, suppose we want to find sum of two numbers and the program is:

“Error in addition from input() function.”‘

Num1 = input(“Enter first number: “)

Num2 = input(“Enter second number: “)

print (‘The sum of ‘, Num1, ‘ and ‘, Num2, ‘ is ‘, Num1+Num2, ‘.’, sep=”) #error

When we execute the above program, it will produce the following output:

Enter first number: 20

Enter second number: 40

The sum of 20 and 40 is 2040.

Here, the program doesn’t add the two numbers together; it just puts one right after the other as 2040. This is because the program does not know that the 10 and 20 are two numbers. Because, Num 1 and Num2 hold two string values. To tell the computer that these are numbers, it is necessary to surround the input function with an int() for integer or a float( ) for floating point number. These are the type names as functions to convert input types.

So, the modified code for above Addl.py program Add2.py is written as:

 “Addition of two number from input() function.“‘

Num1 =int(input(“Enter first number: “))

Num2 =Int(input(“Enter second number: “)))

print (‘The sum of ‘, Num1, ‘ and ‘, Num2, ‘ is ‘, Numl+Num2, ‘.’, sep=”)

When we execute the above program, it will produce the following output:

Enter a number: 20

Enter a second number: 40

The sum of 20 and 40 is 60.

The above can also be written with extra variable as:

“‘Addition of two number from input() function.”‘

Num1 = int(input(“Enter first number: “))

Num2 = int(input(“Enter second number: “))

Sum = Num1 + Num2     #Extra variable to store sum of two numbers

print (‘The sum of ‘, Num1, ‘ and ‘, Num2, ‘ is ‘, Sum, ‘.’, sep=”)