Coding 203: Getting input from a user in Python
In this Learning Lab you will learn how to ask the user for data.
Completion Time: 5 minutes
- Understand how to prompt a user to enter data
- Understand how to use the data entered by the user
- To run the code samples, you need to have Python 3 installed on your machine.
- If you are working on a DevNet Learning Lab PC at a DevNet event, Python 3.4.2 is already installed.
- See BYOD section above for how to install Python on your own machine.
Step 1. Introduction
You often need to ask the user a question, and then use his/her answer to make decisions in an application.
Python makes this operation really easy.
For example, let's ask the user about his favorite vegetable.
We can ask him/her and then display the answer like this.
# ask the user to enter his/her favorite vegetable favorite_veg = input("What's your favorite vegetable? ") # use the input to create a response print("I like " + favorite_veg + " too!")
Fairly simple, right? Let's try it!