How To Create A Dictionary In Python From User Input
- Python user input from the keyboard can be read using the input() built-in function.
- The input from the user is read as a string and can be assigned to a variable.
- After entering the value from the keyboard, we have to press the "Enter" button. Then the input() function reads the value entered by the user.
- The program halts indefinitely for the user input. There is no option to provide timeout value.
- If we enter EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return) , EOFError is raised and the program is terminated.
Syntax of input() Function
The syntax of input() function is:
The prompt string is printed on the console and the control is given to the user to enter the value. You should print some useful information to guide the user to enter the expected value.
Getting User Input in Python
Here is a simple example of getting the user input and printing it on the console.
value = input("Please enter a string:\n") print(f'You entered {value}')
Output:
What is the type of user entered value?
The user entered value is always converted to a string and then assigned to the variable. Let's confirm this by using type() function to get the type of the input variable.
value = input("Please enter a string:\n") print(f'You entered {value} and its type is {type(value)}') value = input("Please enter an integer:\n") print(f'You entered {value} and its type is {type(value)}')
Output:
Please enter a string: Python You entered Python and its type is <class 'str'> Please enter an integer: 123 You entered 123 and its type is <class 'str'>
How to get an Integer as the User Input?
There is no way to get an integer or any other type as the user input. However, we can use the built-in functions to convert the entered string to the integer.
value = input("Please enter an integer:\n") value = int(value) print(f'You entered {value} and its square is {value ** 2}')
Output:
Python user input and EOFError Example
When we enter EOF, input() raises EOFError and terminates the program. Let's look at a simple example using PyCharm IDE.
value = input("Please enter an integer:\n") print(f'You entered {value}')
Output:
Please enter an integer: ^D Traceback (most recent call last): File "/Users/pankaj/Documents/PycharmProjects/PythonTutorialPro/hello-world/user_input.py", line 1, in <module> value = input("Please enter an integer:\n") EOFError: EOF when reading a line
Python User Input Choice Example
We can build an intelligent system by giving choice to the user and taking the user input to proceed with the choice.
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.")
Here is a sample output from the execution of the above program.
Quick word on Python raw_input() function
The raw_input() function was used to take user input in Python 2.x versions. Here is a simple example from Python 2.7 command line interpreter showing the use of raw_input() function.
~ python2.7 Python 2.7.10 (default, Feb 22 2019, 21:55:15) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> value = raw_input("Please enter a string\n") Please enter a string Hello >>> print value Hello
This function has been deprecated and removed from Python 3. If you are still on Python 2.x versions, it's recommended to upgrade to Python 3.x versions.
Conclusion
It's very easy to take the user input in Python from the input() function. It's mostly used to provide choice of operation to the user and then alter the flow of the program accordingly.
However, the program waits indefinitely for the user input. It would have been nice to have some timeout and default value in case the user doesn't enter the value in time.
References:
- Python.org input() documentation
How To Create A Dictionary In Python From User Input
Source: https://www.askpython.com/python/examples/python-user-input
Posted by: lafranceshearompal.blogspot.com
0 Response to "How To Create A Dictionary In Python From User Input"
Post a Comment