HomeBlogProgrammingWhat Is User Input in Python?
Developers need to communicate with users to collect data or to generate results. Many applications today use different ways to request a certain form of input from the user. Python offers us integrated features to take the input. The latest version of Python uses the input() method while the earlier version uses the raw_input() method.
To do this, we have to click the Enter button after entering the value from the keyboard. The input() function then reads the user’s value. The input function reads a line from the console, then converts it to a string, and returns it.
To read about Self in Python, click here!
The syntax of input() function is:
input(prompt)
In the console, the prompt string is printed, and the user is given the command to enter the value. You should print some useful information to help the user enter the expected value.
The input() function receives the user input from the keyboard and stores it into a variable name. Here str is a variable and the print() function prints it to the screen.
The input() function delays execution of a program to allow the user to type the input from the keyboard. On pressing Enter, all characters are reads and returns as a string.
Example:
Here is an example of getting the user input and printing it on the console.
str = input(“Enter your name: “) print(str)
Output:
Enter your name: Joseph Joseph
We will use float() function to convert the received input into a float value.
Example:
value = float(input("Enter a value: ")) print(value)
Output:
Enter a value: 200.99 200.99
The input function converts whatever you enter as an input into a string. If you enter an integer value, the input() function will convert it to a string.
To use the integers, we can use the built-in functions to convert it into an int.
Example:
value =input("Enter an integer:\n") value =int(value) print(f'The entered number is {value} and its square is {value ** 2}')
Output:
Enter an integer: 10 The entered number is 10 and its square is 100
Example:
# 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]
EOFError in python is one of the exceptions to handle errors and is raised in scenarios such as the interruption of the input() function in both python version 2.7 and python version 3.6, as well as other versions after version 3.6 or when the input() function reaches the unexpected end of the file in python version 2.7, i.e. the functions do not read any data before the end of the input is encountered.
Methods such as read() must return a string that is empty at the end of the file, which is inherited in Python from the exception class that is inherited in Base Exception class. This mistake often happens with online IDEs.
If the input() function did not read any data then the python code will throw an exception i.e. EOFError.
value =input("Please enter an integer:\n") print(f'You entered {value}')
Output:
Enter a value: Traceback (most recent call last): File “example.py”, line 1, in <module> value = input(“Please enter an integer: “) EOFError
If End of file Error or EOFError occurs, the exception EOFError is raised without reading any information by the input() function. We should try to enter something like CTRL + Z or CTRL + D before submitting the End of file exception to prevent the raising of this exception.
Example:
try: data = raw_input ("Do you want to continue?: ") except EOFError: print ("Error: No input or End Of File is reached!") data = "" print data
Output:
The input provided by the user is being read which is: Hello this is demonstration of EOFError Exception in Python. The input provided by the user is being read which is: EOF when reading a line.
Explanation: In the above program, try to prevent the EOFError exception with an empty string, which doesn't print the error message of the End Of File and rather prints the personalized message that is shown in the program and prints the same on the output.
You can use the try and catch block if the exception EOFError is processed.
Python provides a multiple-choice input system. You have to first get the keyboard input by calling the input() function. Then evaluate the choice by using the if-elif-else structure.
value1 = input("Please enter first integer:\n") value2 = input("Please enter second integer:\n") v1 = int(value1) v2 = int(value2) choice = input("Enter 1 for addition.\nEnter 2 for subtraction.\nEnter 3 for Multiplication:\n") choice = int(choice) if choice == 1: print(f'You entered {v1} and {v2} and their addition is {v1 + v2}') elif choice == 2: print(f'You entered {v1} and {v2} and their subtraction is {v1 - v2}') elif choice == 3: print(f'You entered {v1} and {v2} and their multiplication is {v1 * v2}') else: print("Wrong Choice, terminating the program.")
Output:
Please enter first integer: 20 Please enter second integer: 10 Enter 1 for addition. Enter 2 for subtraction. Enter 3 for Multiplication: 2 You entered 20 and 10 and their subtraction is 10
Python raw_input function is a built-in function used to get the values from the user. This function is used only in the older versions of Python i.e. Python 2.x version.
Top Cities Where Knowledgehut Conduct Python Certification Course Online
Python 2 must be installed in your system in order to use the raw input() function . Use python2 instead of python or Python3 if you run your program from a terminal. Therefore, the execution sample command is:
$python2 example.py
It depends on how your python have installed. In conclusion, you must run your Python program using the version python 2.x if you use raw_input function.
You can modify your python compiler if you use PyCharm IDE. Go to File -> Configuration -> Project -> Project Interpreter for that purpose. Then select python 2.x from the list.
Example 1:
We will see a code written in Python 2.x version.
name = raw_input(‘Enter your name : ‘) print ‘Username : ‘, name
Output:
Enter your name : Steve Username : Steve
Example 2:
# input() function in Python2.x value1 = raw_input("Enter the name: ") print(type(value1)) print(value1) value2 = raw_input("Enter the number: ") print(type(value2)) value2 = int(value2) print(type(value2)) print(value2)
Output:
Enter the name: Jen
Name | Date | Fee | Know more |
---|