Heads up! To view this whole video, sign in with your Treehouse account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Hello Python!
You have completed Hello Python!
Preview
Utilize Python's input function to ask the user questions.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You've got some coding skills under your
belt now, but the code is a bit one sided.
0:00
You print messages in the console, and
0:06
then the user viewing them just
kind of sits there and reads.
0:09
A lot of great apps and websites are great
because users get to interact with them.
0:13
Let's update our code to
get the users involved.
0:20
And at the top of the file,
we're going to add an input function.
0:23
It looks like this, input,
open parentheses, close parentheses.
0:29
This function prompts the user.
0:36
Let's save if you haven't already and
run the file.
0:39
See how your console cursor is blinking
here and the print messages are missing.
0:49
The program is waiting for
your response to the prompt
0:57
until you respond,
the rest of the program is on hold.
1:01
The only issue,
what are you responding to?
1:07
We'll need to fix that.
1:11
So, the user knows what we want.
1:12
Go ahead and type in anything you want and
then hit Enter.
1:15
Now the rest of our messages
are printed out and
1:17
the program is complete.
1:22
Inside of our input function,
1:27
you can add a message to let the user
know what you're prompting them to do.
1:28
Let's add the string.
1:33
What is your name?
1:35
Save and run the file.
1:45
Up arrow, Enter.
1:49
Great, now users will have a message
to inform them of what they should
1:52
input here.
1:57
One small design issue though,
when you start typing,
1:58
it starts right at the end of the message.
2:02
We can fix this by adding a space or two
to our string inside of the input.
2:05
Type a name and hit Enter and
let's make that change,
2:11
add a space, save and rerun the file.
2:17
There we go.
2:25
All of this is great.
2:29
But to make this program
truly interactive,
2:31
we should use the name the user
inputs in our print messages.
2:34
You can capture the message the user
enters by setting this function.
2:38
Equal to a variable.
2:43
Let's delete the name string here.
2:48
And place input there instead.
2:54
I also don't need these extra lines and
save
3:00
This will run the input function asking
the user to enter their name and
3:06
then save the result to this variable.
3:11
Then our print statement here at
the bottom will use the name that
3:16
they gave us when it prints out the name.
3:20
Save and run the file.
3:24
Now the messages are my name and Megan.
3:27
But it's kind of hard to
tell that we made a change.
3:34
So, let me try a different name.
3:36
My name is Anne Claire.
3:41
Wonderful, let's practice this some
more by creating a small conversation.
3:46
Let's ask them what their last name is.
3:51
If you wanna challenge,
you can pause me and
3:55
try creating a variable and input
pairing to ask what their last name is.
3:58
It should look similar
to our name input here.
4:03
Unpause me when you're finished or
if you get stuck.
4:07
Okay, I'm going to use last, underscore,
4:13
name as the variable
name to be descriptive.
4:16
In Python when you have a long variable
name, you connect them with underscores.
4:20
This is called snake case.
4:25
Last name is equal to input, parentheses,
quotation marks and parentheses,
4:29
what is your last name,
question mark, space and save.
4:36
Now I'm going to create
some new print statements
4:41
to show the user we've
received their last name.
4:46
So, we'll have print, My name is,
print, name, print, last name.
4:53
Save and run the file.
4:58
Input any first name and
last name that you want.
5:09
And wonderful, your program works.
5:17
My name is Grace Hopper.
5:20
Take some time to practice
creating inputs of your own,
5:22
save the result to a variable and
print things out.
5:25
The best way to solidify
knowledge is to practice.
5:29
Keep up the excellent work.
5:33
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up