Originally posted on compucademy.
User input is one of the things which makes Python programming powerful and interesting, as it provides interactivity. This article shows you how to make Python ask for input from the user and how to use that input in your programs.
PYTHON USER INPUT BASIC EXAMPLE
Open up your Python editor and type and run the following code:
my_input = input("Please enter something: ") print("You entered:", my_input)
Please enter something: Hello Computer You entered: Hello Computer
Notice the use of a comma in the
print() command to print multiple items.
input() command in Python creates a prompt for user input in the Python console. We can add a message to make it clear what input is wanted, and this is done by using a string as an argument to
input(), as in
input("Please enter something: "). Notice the colon and the space to stop the input from looking “cramped” in the console. We also need to assign the value input by the user to a variable, so we can use it later in our program.
DATA TYPES FOR PYTHON USER INPUT
Try running this short program:
my_input = input("Please enter something: ") print(my_input * 2)
If you input
4, what do you expect the answer to be?
You might be surprised by the output:
44. Why do we get
44 instead of
8? The answer is that
my_input is actually a string, and when we “add” two strings, they get joined, or concatenated. If we want to work with numeric input, we need to convert our input to an appropriate data type first, such as an
int (whole number) or
float (number with decimal point).
To get the desired behaviour of multiplying
2 we use the
int() method on our input, like so:
my_input = int(input("Please enter something: ")) print(my_input * 2) print(type(my_input))
Please enter something: 4 8 <class 'int'>
We get a
ValueErrorif we try and convert non-numeric input to an
PYTHON USER INPUT ACTIVITY – ADDER PROGRAM
Write a program which asks for two numbers and prints the result of adding them together. For now you may assume the user will enter actual numbers, so you don’t need to check. Don’t forget to provide meaningful messages for both the input and the output. So something like
Enter a number: and
5 + 4 = 9.
PYTHON USER INPUT TIPS
input("Press Enter to continue.")to pause a program and wait for the user to press
It is often a good idea to covert input to lowercase, so that capitalization doesn’t cause unexpected results. You can do this with
Are you a teacher?
Check out this teaching pack covering:
- Reading user input from the console
- Converting user input to the appropriate data type
- Working with user input inside loops
- Validating user input
- Slide-show on user input with Python
- Student worksheet for working with user input in Python, including coding exercises and extension activities
- Python .py files for all activities and exercise solutions
- Cover sheet
PYTHON USER INPUT CHALLENGE 1
- Write a program that asks for a password and displays “Success” if the password is correct, and “Access denied” if not.
PYTHON USER INPUT CHALLENGE 2
- Write some python code to keep checking that a password entered by the user is correct. You will probably want to use a
whileloop for this.
VALIDATION OF USER INPUT
On very important aspect of working with user input is validation. This means that we check our input against some essential criteria which it must possess. For example, the input may need to be numeric, or within a certain range, or meet any number of other criteria. You have probably come across validation of user input many times when filling in online forms and finding that for some reason the input you gave was not accepted.
VALIDATION OF USER INPUT ACTIVITY 1
Make a list of at least 5 different examples of validation of user input which you have experienced or which you can think of.
VALIDATION OF USER INPUT PYTHON PROGRAMMING ACTIVITY
Type the code below into your Python editor, and complete it, so that it keeps asking for input until an integer is entered. A helpful method you can use to check if the input consist of digits is
# Initialise my_num my_num = None # Loop control variable waiting = True while waiting: # Get input from console # Check if input is only digits using isdigit() # Display accepted value print("Value:", value)
Solutions for the above activities are provided in the Python User Input Teaching Pack described above.
You now have some experience of working with user input in Python. These basics will serve you well as you write your own interactive programs. I encourage you to come up with your own ideas for programs which require user input and practice the ideas and techniques we have covered here.