Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Basics!
You have completed Python Basics!
Preview
Let's explore how to take input from users of our programs
Learn More
- PEP8 - Prescriptive Naming Conventions (Immerse yourself! You don't need to understand all of it!)
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
So far, what we have done is all output.
0:00
We printed the screen,
it's not very interactive, is it?
0:03
The conversation's kind of one sided.
0:06
Take a second and
think about applications that you use.
0:09
Just about every one of them gathers
information from you, whether that be
0:12
a field, or a check box, a button click,
or telling a bird when to flap its wings.
0:16
Every application you make is going to
need to gather some information from its
0:21
users, this is called input.
0:25
And gathering input from users from the
command line is pretty straight forward,
0:28
so let's get to it.
0:32
So let's explore this input thing
a little bit in the Python shell first.
0:33
So I'm gonna open that up,
we'll type python.
0:37
Let's give ourselves
a little bit of space.
0:42
All right, so the command to get input
from the user is super easy to remember,
0:44
it's input.
0:50
So input, that's a function,
so we're gonna call it.
0:51
And inside here you put the prompt.
0:56
So let's ask a question, so
let's say, How are you today?
0:58
So see how the cursor is here,
it's waiting for me to type, so how am I?
1:05
Great, I mean you're standing a little
close to me, but pretty great.
1:09
Already we're seeing the importance
of user experience right?
1:14
That prompt made me feel uncomfortable.
1:17
So I'm gonna press Enter.
1:19
And now, remember,
in the REPL we see the results show up.
1:21
So what happens is when you call the input
function, it outputs out the prompt and
1:24
then it waits for an answer.
1:29
Once the answer to the prompt is given,
and
1:31
the user presses Enter,
the result is returned.
1:34
So we know that this input
returns a string, so
1:38
let's capture it in a new variable.
1:41
So let's see,
what would how are you today be?
1:43
[SOUND] I guess that'd probably be
something like current mood, right?
1:47
[LAUGH] Naming things is hard, isn't it?
1:52
So we do current,
1:54
and then we're gonna use the underscore,
cuz there's two words, right?
1:55
So current_mood,
that's current mood in snake case.
1:57
And we'll say, again,
we do the input, How are you today?
2:00
Now, you know what, I'm gonna use couple
of spaces just to give some room there for
2:06
user experience.
2:11
So let's do this.
2:13
So how are you today?
2:13
Well, I am wonderful, especially
[LAUGH] now that you're not standing so
2:16
close to me.
2:19
And now our variable, current_mode,
if we just type that out here,
2:21
we can see what's in it, wonderful.
2:25
Awesome, so you know what,
we could just put this in our program.
2:28
We could ask for
the user's first name, so let's do it.
2:31
So we'll just replace this
value with the input statement.
2:35
We'll say, what is your first name?
2:40
And then we'll definitely
give them some space,
2:43
cuz we want them to feel comfortable,
awesome.
2:45
So now, you know what else,
let's scroll this down,
2:47
let's get rid of these other lines
in here where we're saying hello.
2:51
So I'm gonna Cmd+Shift+D that and
Cmd+Shift+D this,
2:54
and let's bring this back up.
2:58
There we go, that's looking good.
3:00
And it's saved, so let's go ahead,
I'm gonna drop out of the shell, Ctrl+D.
3:01
I'm gonna clear the console, and
I'm gonna type python hello.py.
3:05
So what is your first name?
3:11
I just got an email from one of my
students, so he's on my mind right now.
3:14
So I'll enter Kehinde.
3:16
Hello, Kehinde,
Kehinde is learning Python.
3:23
How's the weather in Nigeria
this time of year, Kehinde?
3:27
And so you'll see that the first
name variable is now set to
3:30
whatever we enter from input.
3:35
So the program changes based on our
user's input, which is awesome.
3:37
Because now we're dealing with input and
output, the staples of application
3:42
development, great job.
3:47
We stumbled right into a fundamental
problem that you're gonna encounter daily
3:49
in your coding journey,
naming things is hard.
3:54
Now you'll get better at it,
[LAUGH] but it's never easy.
3:57
And I can't stress this enough,
a good name is really important.
4:00
You wanna be able to look at your code and
understand and remember what it's doing.
4:05
You wanna be able to read it next week.
4:10
Now one thing to remember, and most people
who are just getting started coding
4:12
don't think about this too often,
other people are going to read your code.
4:16
Now, that might seem like a totally
foreign idea, but ready for this?
4:21
You're going to be most likely on a team,
and
4:25
your teammates will need to understand
what you are trying to communicate.
4:28
We already know that
the computer has rules for
4:32
understanding what we're talking about,
right?
4:34
That's called syntax.
4:37
But what we're talking
about here is coding style.
4:38
[SOUND] There is absolutely nothing
stopping you from using a single letter
4:40
variable name for
the current mood code that we wrote.
4:44
Like I could store the result from
the prompt, how are you today,
4:48
in a variable called m.
4:51
But, I can guarantee that your actual
real life mood is going to change for
4:54
the worst, when you read this code and
4:58
have to try to figure out,
what does m mean?
5:00
Great news though, the Python community as
a whole are very big on consistent style.
5:03
Chances are after you
learn the style rules,
5:08
your code will be indistinguishable
from that of a long time Python coder.
5:11
I'll point these standard style
decisions out as we encounter them.
5:16
The one we've seen already is use
snake_case for variable names.
5:19
Now it's definitely not a syntax error,
you could use another casing system,
5:23
like for instance,
you could say currentMood.
5:27
This is called camel case,
each word here has a new hump.
5:30
[SOUND]
thisIsAnExampleOfWhatCamelCasingLooksLike.
5:33
If you're working with a team on
a project, you'll wanna make sure that you
5:38
follow the same style that is
being used in the project.
5:42
Try to stay consistent.
5:45
So, how are you feeling?
5:46
You've picked up quite a bit
of information over these
5:48
past couple of videos.
5:50
I know that it's a lot,
especially if this is your first language.
5:52
So let me say again,
great job sticking with it.
5:54
What you've been learning will give you
a great foundation to dive deeper and
5:57
explore the language.
6:00
We've got the starts of what every
application has, input and output.
6:02
Next up, let's start processing
that input that we receive,
6:07
right after this quick break.
6:10
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