Coming back to our Repl.it,
the Day 1 printing repl that you should have from previously,
I want to talk about a different function.
So we've seen the print function and all the things that we can do to strings
and use the print function and debug it.
But what if we wanted to be able to enter some data?
So if we wanted to say,
ask the user 'what is your name?' And we run this code,
you can see that being printed,
but there's no way for the user on this side to be able to give our code some
data to work with. In order to do that,
instead of using the print function,
we're going to use a different function and it's called the input function.
And notice how as I'm typing,
code intelligence is already giving me some suggestions because it thinks it
knows what I might want and it's right.
So this is what the input function looks like. This is the name of the function.
And again, it's followed by some parentheses. Inside the parentheses
is the prompt that I'm going to give the user.
So when I run this code, it will say, what is your name?
And then the cursor will stay at the end of this line because it's expecting
some sort of input. If I give it my name, Angela and I hit enter,
then this piece of data has now been passed back into my code and it now
replaces this part of the code.
So I'm passing data from over here,
back to over here where I can use it in my code.
Now notice the difference here. It looks quite similar.
When we run print, you can see it says what is your name?
and then the code execution ends and we know it ends because it's showing us
this little orange arrow.
But notice when I run input instead,
I don't see the orange arrow.
My program is actually paused right now in order for the user to provide an
input and it's only when I've given the input and hit enter
does the program end.
The input function looks pretty much identical to the print function,
but instead of the word print,
it's just got the word input. And inside the parentheses,
instead of adding what text will be printed,
we're adding the prompt for the user to give them a hint as to what kind of data
we want. And then when you run this code, it will print out the prompt,
but then there will be a cursor. In some places you'll see it like a flashing
cursor, other places,
it'll just be a solid cursor ready for the user to type in some piece of data.
So what can we do with this data? Well,
we can use it inside our code.
Remember how I said once this line of code executes and I've entered Angela as
the response to this input,
then this part of the code gets replaced by whatever it is that the user typed
in. So in this case it becomes Angela.
And what I can do with it is let's say I wanted to print something like,
hello Angela. Well,
then I can simply write Hello + input and then I'm going to close it off
with another set of brackets.
So I'm going to change my layout instead of side by side,
I'm going to change it to stacked.
And this means that I've got my code at the top and my console at the bottom.
This way my line can go a little bit longer without it rapping.
Notice what's happening here.
This is the input function that I showed you earlier on and this is a print
statement.
So we've got this nested inside the other. And now when I run my code,
what happens is it asked me what is your name?
So I'll write my name and then when I hit enter,
this part is replaced by Angela and it gets concatenated to the word hello and
the entire thing gets printed out down here.
If you find this line of code difficult to understand and to wrap your head
around,
then I recommend heading over to a website called thonny.org and downloading an
application called Thonny. It's completely free and it's available for Windows,
Mac, and Linux. And once you've installed this application,
you can go ahead and paste your line of code here and click on this little debug
symbol. And then we can click on this button,
which is called the step into button to step into the execution of this line of
code
to see how the computer is evaluating this code here step by step.
So you'll see the first thing it tries to do is it will try to run this print
statement. It looks inside, these parentheses to see what it needs to print.
So the first thing is this, Hello,
and it turns that into a string and then it looks at the next thing after the
plus sign to see what it should turn this into.
And this of course is the input function. So as I continue stepping into it,
it's actually going to execute it.
So it's going to run this input function and show the prompt, what is your name?
So that's what shows up down here.
And now if I enter a value in here and hit enter,
then that value that I put right there replaces that previous input function
and is now held inside the print statement.
So if I continue stepping through this code then you'll see it concatenates all of
the pieces together, and finally it just ends up with a simple print
Hello Angela! And if I continue stepping into it,
you'll see that line of code executed until there are no more instructions left.
So if you prefer seeing your code, um,
run like this step by step so that you can see what's going on,
then I recommend giving this a go.
But because of how easy it is to share these Repl.it code bases so that we're
working from the same place, I recommend to do most of your coding here
and only if you get stuck to take a single line of code and paste it into Thonny
to see how it's executed step-by-step. As you're learning to code,
there's going to be new concepts covered.
There's going to be things that take a little bit of thinking before you can
understand it.
So what a lot of programmers love to do is they like to comment in their code.
Now,
you might've seen this already throughout the coding exercises and the solutions
I provided. But in Python,
if you wanted a line of text to not be considered by the computer at all,
all you have to do is add the hashtag or pound sign in front of your text and
this turns it into a comment,
so something that the computer will completely ignore.
So in here you can write code as much as you like, um,
but it won't be executed and when you click run,
you'll see that this is completely ignored.
What I recommend is whenever you come across a new concept in the course to make
a comment above it so that you can explain to yourself what's actually going on
in the line of code below. This way
the next time you come across this line of code and you're not quite sure what's
actually happening here,
you can have a look at the notes that you've written for yourself in your own
words and hopefully it'll re-jog your memory and make it much easier to
comprehend what's going on.
In addition to adding a pound sign manually,
you can also highlight a line of code or simply have your cursor on a line of
code and hold down command and forward slash if you're on Mac or control and
forward slash if you're on Windows.
And if you want to go back or undo your changes,
it's simply command +z or control + z. All right,
so in this lesson we learned about input function,
we learned about putting functions inside other functions and being able to get
user input from the console by using the input function.
So now I've got a coding exercise for you in the next lesson,
and if you've commented this line of code thoroughly and you fully understood
how it works, then you'll be able to breeze through the next exercise.
So for all of that and more, I'll see you there.