Now, if you head over to task number three, in today's set of exercises, you'll see the code that
we had from previously.
And today 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.
Now this time you'll notice a couple of differences.
Firstly it doesn't have that final line process finished with exit code zero.
It's actually not saying anything at all.
Plus, when you look up here, you'll see that in addition to the play button, there's the stop button
being highlighted and the play button has changed this rerun symbol.
So what does all of this mean?
Well, it means that we're actually in the middle of the process of running our code.
It hasn't exited yet.
And what exactly is it waiting for?
Well, it's waiting for us to click into this output pane and actually type in an input.
So the prompt is the text that goes into the input command, and then afterwards we can add some actual
input, whatever it is we want into the output area.
And now once I hit enter, you'll see that finally we have the process finished with exit code.
You'll see the stop button is gone and we can now rerun the code as usual.
But where did this text go and how can we capture it?
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.
And in some places you won't even see a cursor until you click into the output area 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.
Now the way that this works is when this line is run by the computer, it takes your command as understanding
that you want the user.
So somebody to be able to you want the user to be able to type in an input into this area and whatever
it is they type, you're going to take it back into the code and replace this command with it.
So this is a little bit abstract when we see it like this.
But let's imagine we wanted to write a print statement and we wanted to say hello.
And we wanted to greet the user.
Now I know that in my case my name is Angela.
So I would get it to say hello Angela.
But what if you didn't know the name of your user.
Well, you wouldn't just want to say hello user or hello something generic.
You would want them to be able to type in their name.
So this is where a little bit of string concatenation comes in, which we learned before.
If we take this input function that we've explored just now, and we paste it into this print statement.
So this can be a little bit confusing.
So just concentrate right here.
There's two parentheses at the end.
And the reason for this is because this first one closes off the print statement.
So we're saying everything between here and here.
We want it to be printed.
Now what is in between there.
Well firstly it's the string that says hello and then a space and then what we had as the input.
So whatever the user types into here will replace this highlighted part.
And we'll end up with hello concatenate.
So add to another string.
So let's try this out.
Let's go ahead and hit run and you can see the prompt show up in the output area.
So now if I click into this area, my cursor starts flashing and I can type whatever it is I want.
Now when I hit enter, what I type is going to go and replace this input function.
And then once it's in there, the print function is going to carry out its role and it's going to add
the word hello in front of it.
And then print it out right here so we can see now hello Angela.
But this is of course making our code a lot more adaptable.
So if tomorrow I decided to change my name to a cherry.
There's a really embarrassing story about this.
So when I first moved to the UK, I wanted to pick my own name because I was nine years old and I wanted
a very, uh, fruit related name.
Um, and this was the name that I wanted.
And my parents, as, as wise as they are, said, no, we will take your name.
So that's how I ended up with the name Angela.
So now you can see this gets added in to the hello and we can have a completely adaptable bit of code.
So now it's time for a small challenge.
Can you use what you've learnt from previous lessons and also this lesson to figure out how to add a
exclamation mark to the end of the sentence that's being printed.
So instead of just hello Angela, we want the output to say hello.
And then we take in whatever it is the user inputs into here.
And then we replace that with the user input.
And finally we add an exclamation mark.
Now of course we have to do this using strings and using what we've learned about Python code.
So pause the video, read the description of the problem and see if you can solve it.
Okay, so if you remember from string Catenation.
What we need to do is we need to treat this input function as its own string, because eventually it's
going to be replaced by a string.
So we can take that part, set it aside, and then we can use the plus sign to concatenate the final
character, which is a exclamation mark to this entire long string.
So now if I go ahead and run this code and I type in an input, that input gets inserted and replaces
this entire input function.
And then finally I get the exclamation mark added to the end.
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!
Exclamation mark.
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.
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 have 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, 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 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 what this does is it takes this line of code out of the computer's awareness, and it's no longer
executed when you run this code file.
So the process is finished without anything being carried out at all, because all the computer is seeing
is a blank page, because we've taken all the code out into comments.
So in order to take this back into code, all you have to do is use the same command that you tried
just now.
So command forward slash on Mac or control forward slash on windows.
And this will now again be considered as code by the computer.
All right.
So in this lesson we learned about the 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.
And if you've commented this line of code thoroughly and you've 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.