Skip to 0 minutes and 4 secondsSo far, you've only used print to output some text. Next, you're going to use input to prompt the user to enter some data. Now, input's going to display a message on the screen. The program's then going to wait for the user to give a response and press Enter. And not response then will be stored as a variable so you can use it in your program. Print and input are examples of functions. Functions are pre-made programs which are typically created to perform a single task. Now, Python has many in-built functions, allowing you to perform a large range of different tasks. Now, let's add some code to the bottom of your program to use input to capture the user's name.
Skip to 0 minutes and 38 secondsI'm going to create my variable, called user's name. I'm going to say it's going to be equal to input. I'm going to open a bracket and I'm going to put the phrase that I want the user to see. Enter your name. Now, let's just break down this line of code. Uses_name equals is going to create a variable. And that variable is going to be assigned the value that input gives me. Input is obviously in the name of the function. And between the brackets is the phrase that's going to be given to the user. Now we've used input to capture the user's name. Let's just print it to the screen.
Skip to 1 minute and 23 secondsNow, let's run our program and see what happens. So we've run our program. I get a prompt saying enter your name. I'm going to put Martin. And I'm going to press Enter. And it prints out my name because that's what's held in the users_names variable. But it's not a very friendly message. Let's improve that by adding to the front of it, hello, users_name. Welcome. And again, let's run our program to see what happens.
Skip to 1 minute and 55 secondsEnter your name. Martin. Hello, Martin. Welcome. So why not grab a friend, run your program, and ask them to input their name? Have some fun with it.
Getting input from the user
So far you have only been using
In this section you will be using
input to prompt the user to enter some data.
input will put a message on the screen, wait for the user to give a response and press Enter, and then store the response as text in a variable so you can use it in your program.
input are examples of functions. Functions are pre-made programs which are typically created to perform a single task. Python has many inbuilt functions allowing you to perform a large range of tasks.
- Add the following code to the bottom of your program to use
inputto capture the user’s name and store it in a variable.
users_name = input("What is your name? ")
Breaking down this line of code you can see what each element does:
users_name =creates a variable called
users_nameand assigns it a value
input()uses the function
inputto display a prompt and capture the user’s response
"What is your name? "– the value between the
()– is the prompt which will be output to the screen
Now the text the user entered is stored in an variable (
users_name) you can use the data in your program.
- Lets use
- Improve the output by adding some text to the start and end of the message.
print("Hello " + users_name + ", welcome.")
- Grab a friend, run your program and ask them to input their name.
Note: you should enter your name in the REPL after the “What is your name?” prompt and press Enter when you have done so.