WEBVTT

00:00.230 --> 00:07.580
Now, if you head over to task number three, in today's set of exercises, you'll see the code that

00:07.580 --> 00:09.500
we had from previously.

00:10.520 --> 00:14.240
And today I want to talk about a different function.

00:14.240 --> 00:21.830
So we've seen the print function and all the things that we can do to strings and use the print function

00:21.830 --> 00:23.180
and debug it.

00:23.180 --> 00:28.100
But what if we wanted to be able to enter some data.

00:28.130 --> 00:32.660
So if we wanted to say ask the user what is your name?

00:33.230 --> 00:40.550
And we run this code, you can see that being printed, but there's no way for the user on this side

00:40.550 --> 00:44.420
to be able to give our code some data to work with.

00:44.480 --> 00:51.080
In order to do that, instead of using the print function, we're going to use a different function,

00:51.080 --> 00:53.660
and it's called the input function.

00:53.660 --> 01:01.220
And notice how as I'm typing code, intelligence is already giving me some suggestions because it thinks

01:01.220 --> 01:03.740
it knows what I might want and it's right.

01:03.740 --> 01:07.140
So this is what the input function looks like.

01:07.170 --> 01:09.000
This is the name of the function.

01:09.000 --> 01:11.850
And again it's followed by some parentheses.

01:11.880 --> 01:16.410
Inside the parentheses is the prompt that I'm going to give the user.

01:16.440 --> 01:20.670
So when I run this code it will say what is your name.

01:20.940 --> 01:24.600
Now this time you'll notice a couple of differences.

01:24.600 --> 01:30.390
Firstly it doesn't have that final line process finished with exit code zero.

01:30.420 --> 01:33.270
It's actually not saying anything at all.

01:33.300 --> 01:39.330
Plus, when you look up here, you'll see that in addition to the play button, there's the stop button

01:39.330 --> 01:44.190
being highlighted and the play button has changed this rerun symbol.

01:44.220 --> 01:46.590
So what does all of this mean?

01:46.620 --> 01:52.560
Well, it means that we're actually in the middle of the process of running our code.

01:52.560 --> 01:54.510
It hasn't exited yet.

01:54.510 --> 01:56.850
And what exactly is it waiting for?

01:56.880 --> 02:04.200
Well, it's waiting for us to click into this output pane and actually type in an input.

02:04.230 --> 02:13.530
So the prompt is the text that goes into the input command, and then afterwards we can add some actual

02:13.530 --> 02:17.100
input, whatever it is we want into the output area.

02:17.100 --> 02:23.190
And now once I hit enter, you'll see that finally we have the process finished with exit code.

02:23.190 --> 02:29.820
You'll see the stop button is gone and we can now rerun the code as usual.

02:30.150 --> 02:35.730
But where did this text go and how can we capture it?

02:35.970 --> 02:42.600
The input function looks pretty much identical to the print function, but instead of the word print,

02:42.600 --> 02:46.980
it's just got the word input and inside the parentheses.

02:46.980 --> 02:54.150
Instead of adding what text will be printed, we're adding the prompt for the user to give them a hint

02:54.150 --> 02:56.010
as to what kind of data we want.

02:56.040 --> 03:02.190
And then when you run this code, it will print out the prompt, but then there will be a cursor.

03:02.190 --> 03:08.130
In some places you'll see it like a flashing cursor, other places it'll just be a solid cursor.

03:08.280 --> 03:15.310
And in some places you won't even see a cursor until you click into the output area ready for the user

03:15.310 --> 03:17.740
to type in some piece of data.

03:18.310 --> 03:20.890
So what can we do with this data?

03:20.980 --> 03:24.430
Well, we can use it inside our code.

03:24.970 --> 03:34.060
Now the way that this works is when this line is run by the computer, it takes your command as understanding

03:34.060 --> 03:35.980
that you want the user.

03:35.980 --> 03:45.400
So somebody to be able to you want the user to be able to type in an input into this area and whatever

03:45.400 --> 03:52.960
it is they type, you're going to take it back into the code and replace this command with it.

03:52.990 --> 03:56.650
So this is a little bit abstract when we see it like this.

03:56.650 --> 04:01.870
But let's imagine we wanted to write a print statement and we wanted to say hello.

04:01.870 --> 04:03.820
And we wanted to greet the user.

04:03.850 --> 04:07.300
Now I know that in my case my name is Angela.

04:07.300 --> 04:09.910
So I would get it to say hello Angela.

04:09.910 --> 04:13.150
But what if you didn't know the name of your user.

04:13.150 --> 04:17.290
Well, you wouldn't just want to say hello user or hello something generic.

04:17.320 --> 04:20.230
You would want them to be able to type in their name.

04:20.230 --> 04:25.270
So this is where a little bit of string concatenation comes in, which we learned before.

04:25.300 --> 04:33.910
If we take this input function that we've explored just now, and we paste it into this print statement.

04:34.180 --> 04:36.340
So this can be a little bit confusing.

04:36.340 --> 04:38.260
So just concentrate right here.

04:38.260 --> 04:40.540
There's two parentheses at the end.

04:40.540 --> 04:45.520
And the reason for this is because this first one closes off the print statement.

04:45.520 --> 04:48.310
So we're saying everything between here and here.

04:48.310 --> 04:50.110
We want it to be printed.

04:50.140 --> 04:52.000
Now what is in between there.

04:52.030 --> 04:59.650
Well firstly it's the string that says hello and then a space and then what we had as the input.

04:59.650 --> 05:04.840
So whatever the user types into here will replace this highlighted part.

05:04.840 --> 05:07.930
And we'll end up with hello concatenate.

05:07.930 --> 05:10.540
So add to another string.

05:10.540 --> 05:12.670
So let's try this out.

05:13.630 --> 05:20.330
Let's go ahead and hit run and you can see the prompt show up in the output area.

05:20.360 --> 05:27.920
So now if I click into this area, my cursor starts flashing and I can type whatever it is I want.

05:27.950 --> 05:35.210
Now when I hit enter, what I type is going to go and replace this input function.

05:35.210 --> 05:42.110
And then once it's in there, the print function is going to carry out its role and it's going to add

05:42.110 --> 05:43.580
the word hello in front of it.

05:43.580 --> 05:48.440
And then print it out right here so we can see now hello Angela.

05:48.440 --> 05:51.920
But this is of course making our code a lot more adaptable.

05:51.920 --> 05:58.400
So if tomorrow I decided to change my name to a cherry.

05:58.490 --> 06:00.770
There's a really embarrassing story about this.

06:00.770 --> 06:07.310
So when I first moved to the UK, I wanted to pick my own name because I was nine years old and I wanted

06:07.310 --> 06:11.540
a very, uh, fruit related name.

06:12.080 --> 06:15.590
Um, and this was the name that I wanted.

06:15.590 --> 06:20.810
And my parents, as, as wise as they are, said, no, we will take your name.

06:20.810 --> 06:23.630
So that's how I ended up with the name Angela.

06:23.990 --> 06:33.200
So now you can see this gets added in to the hello and we can have a completely adaptable bit of code.

06:33.530 --> 06:37.160
So now it's time for a small challenge.

06:37.160 --> 06:45.680
Can you use what you've learnt from previous lessons and also this lesson to figure out how to add a

06:45.680 --> 06:51.080
exclamation mark to the end of the sentence that's being printed.

06:51.080 --> 06:55.880
So instead of just hello Angela, we want the output to say hello.

06:55.880 --> 07:00.350
And then we take in whatever it is the user inputs into here.

07:00.350 --> 07:03.500
And then we replace that with the user input.

07:03.500 --> 07:06.290
And finally we add an exclamation mark.

07:06.290 --> 07:11.720
Now of course we have to do this using strings and using what we've learned about Python code.

07:11.720 --> 07:17.240
So pause the video, read the description of the problem and see if you can solve it.

07:17.240 --> 07:20.610
Okay, so if you remember from string Catenation.

07:20.610 --> 07:28.980
What we need to do is we need to treat this input function as its own string, because eventually it's

07:28.980 --> 07:30.570
going to be replaced by a string.

07:30.570 --> 07:37.920
So we can take that part, set it aside, and then we can use the plus sign to concatenate the final

07:37.920 --> 07:44.160
character, which is a exclamation mark to this entire long string.

07:44.160 --> 07:52.020
So now if I go ahead and run this code and I type in an input, that input gets inserted and replaces

07:52.020 --> 07:54.030
this entire input function.

07:54.030 --> 07:58.530
And then finally I get the exclamation mark added to the end.

07:58.800 --> 08:05.040
If you find this line of code difficult to understand and to wrap your head around, then I recommend

08:05.070 --> 08:06.960
heading over to a website called thonny.

08:07.170 --> 08:10.440
Org and downloading an application called Thonny.

08:10.470 --> 08:14.250
It's completely free and it's available for windows, Mac and Linux.

08:14.250 --> 08:22.200
And once you've installed this application, you can go ahead and paste your line of code here and click

08:22.200 --> 08:24.340
on this little debug symbol.

08:24.490 --> 08:31.120
And then we can click on this button which is called the step into button, to step into the execution

08:31.120 --> 08:37.690
of this line of code to see how the computer is evaluating this code here step by step.

08:37.720 --> 08:42.340
So you'll see the first thing it tries to do is it will try to run this print statement.

08:42.340 --> 08:46.060
It looks inside these parentheses to see what it needs to print.

08:46.060 --> 08:48.790
So the first thing is this hello.

08:48.790 --> 08:51.190
And it turns that into a string.

08:51.310 --> 08:56.650
And then it looks at the next thing after the plus sign to see what it should turn this into.

08:56.680 --> 08:58.870
And this of course is the input function.

08:58.870 --> 09:03.520
So as I continue stepping into it it's actually going to execute it.

09:03.520 --> 09:07.270
So it's going to run this input function and show the prompt.

09:07.270 --> 09:08.560
What is your name.

09:08.950 --> 09:10.960
So that's what shows up down here.

09:10.960 --> 09:19.750
And now if I enter a value in here and hit enter, then that value that I put right there replaces that

09:19.810 --> 09:24.430
previous input function and is now held inside the print statement.

09:24.430 --> 09:27.790
So if I continue stepping through this code.

09:27.790 --> 09:31.300
Then you'll see it concatenates all of the pieces together.

09:31.300 --> 09:34.540
And finally it just ends up with a simple print.

09:34.570 --> 09:35.530
Hello, Angela!

09:35.560 --> 09:36.760
Exclamation mark.

09:36.760 --> 09:43.210
And if I continue stepping into it, you'll see that line of code executed until there are no more instructions

09:43.210 --> 09:43.840
left.

09:43.840 --> 09:50.110
So if you prefer seeing your code, um, run like this step by step so that you can see what's going

09:50.110 --> 09:52.660
on, then I recommend giving this a go.

09:53.500 --> 09:57.490
As you're learning to code, there's going to be new concepts covered.

09:57.520 --> 10:01.990
There's going to be things that take a little bit of thinking before you can understand it.

10:01.990 --> 10:07.450
So what a lot of programmers love to do is they like to comment in their code.

10:07.480 --> 10:12.730
Now, you might have seen this already throughout the coding exercises and the solutions I provided,

10:12.730 --> 10:21.070
but in Python, if you wanted a line of text to not be considered by the computer at all, all you have

10:21.070 --> 10:29.900
to do is add the hashtag or pound sign in front of your text, and this turns it into a comment.

10:29.900 --> 10:33.440
So something that the computer will completely ignore.

10:33.440 --> 10:39.020
So in here you can write code as much as you like, but it won't be executed.

10:39.020 --> 10:43.100
And when you click run, you'll see that this is completely ignored.

10:43.130 --> 10:49.460
What I recommend is whenever you come across a new concept in the course, to make a comment above it

10:49.460 --> 10:55.760
so that you can explain to yourself what's actually going on in the line of code below.

10:56.150 --> 11:01.430
This way, the next time you come across this line of code and you're not quite sure what's actually

11:01.430 --> 11:06.740
happening here, you can have a look at the notes that you've written for yourself in your own words,

11:06.740 --> 11:11.990
and hopefully it'll jog your memory and make it much easier to comprehend what's going on.

11:13.010 --> 11:21.290
In addition to adding a pound sign manually, you can also highlight a line of code or simply have your

11:21.290 --> 11:26.180
cursor on a line of code and hold down command and forward slash.

11:26.180 --> 11:30.950
If you're on Mac or Control and forward slash if you're on windows.

11:30.950 --> 11:38.120
And what this does is it takes this line of code out of the computer's awareness, and it's no longer

11:38.120 --> 11:40.700
executed when you run this code file.

11:40.700 --> 11:46.880
So the process is finished without anything being carried out at all, because all the computer is seeing

11:46.880 --> 11:52.340
is a blank page, because we've taken all the code out into comments.

11:52.370 --> 11:58.160
So in order to take this back into code, all you have to do is use the same command that you tried

11:58.160 --> 11:58.760
just now.

11:58.760 --> 12:03.530
So command forward slash on Mac or control forward slash on windows.

12:03.530 --> 12:08.090
And this will now again be considered as code by the computer.

12:08.720 --> 12:09.530
All right.

12:09.530 --> 12:12.920
So in this lesson we learned about the input function.

12:12.920 --> 12:19.610
We learned about putting functions inside other functions and being able to get user input from the

12:19.610 --> 12:22.490
console by using the input function.

12:22.520 --> 12:29.180
And if you've commented this line of code thoroughly and you've fully understood how it works, then

12:29.210 --> 12:32.120
you'll be able to breeze through the next exercise.

12:32.120 --> 12:35.210
So for all of that and more, I'll see you there.
