Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,270 --> 00:00:02,550
Coming back to our Repl.it,
2
00:00:02,760 --> 00:00:07,230
the Day 1 printing repl that you should have from previously,
3
00:00:08,040 --> 00:00:10,410
I want to talk about a different function.
4
00:00:10,530 --> 00:00:15,530
So we've seen the print function and all the things that we can do to strings
5
00:00:15,870 --> 00:00:19,230
and use the print function and debug it.
6
00:00:19,710 --> 00:00:24,090
But what if we wanted to be able to enter some data?
7
00:00:24,630 --> 00:00:26,220
So if we wanted to say,
8
00:00:26,220 --> 00:00:31,020
ask the user 'what is your name?' And we run this code,
9
00:00:31,410 --> 00:00:33,180
you can see that being printed,
10
00:00:33,570 --> 00:00:38,570
but there's no way for the user on this side to be able to give our code some
11
00:00:39,150 --> 00:00:42,270
data to work with. In order to do that,
12
00:00:42,630 --> 00:00:44,970
instead of using the print function,
13
00:00:45,270 --> 00:00:49,500
we're going to use a different function and it's called the input function.
14
00:00:50,250 --> 00:00:52,470
And notice how as I'm typing,
15
00:00:52,710 --> 00:00:57,570
code intelligence is already giving me some suggestions because it thinks it
16
00:00:57,570 --> 00:00:59,670
knows what I might want and it's right.
17
00:01:00,180 --> 00:01:05,070
So this is what the input function looks like. This is the name of the function.
18
00:01:05,460 --> 00:01:09,300
And again, it's followed by some parentheses. Inside the parentheses
19
00:01:09,300 --> 00:01:12,270
is the prompt that I'm going to give the user.
20
00:01:12,990 --> 00:01:16,590
So when I run this code, it will say, what is your name?
21
00:01:17,100 --> 00:01:22,100
And then the cursor will stay at the end of this line because it's expecting
22
00:01:22,950 --> 00:01:27,300
some sort of input. If I give it my name, Angela and I hit enter,
23
00:01:27,720 --> 00:01:32,720
then this piece of data has now been passed back into my code and it now
24
00:01:33,600 --> 00:01:36,360
replaces this part of the code.
25
00:01:36,780 --> 00:01:39,180
So I'm passing data from over here,
26
00:01:39,540 --> 00:01:42,480
back to over here where I can use it in my code.
27
00:01:43,680 --> 00:01:46,890
Now notice the difference here. It looks quite similar.
28
00:01:47,100 --> 00:01:50,940
When we run print, you can see it says what is your name?
29
00:01:51,390 --> 00:01:56,190
and then the code execution ends and we know it ends because it's showing us
30
00:01:56,220 --> 00:01:57,720
this little orange arrow.
31
00:01:58,350 --> 00:02:01,290
But notice when I run input instead,
32
00:02:01,830 --> 00:02:03,570
I don't see the orange arrow.
33
00:02:03,750 --> 00:02:08,750
My program is actually paused right now in order for the user to provide an
34
00:02:09,060 --> 00:02:13,200
input and it's only when I've given the input and hit enter
35
00:02:13,680 --> 00:02:15,090
does the program end.
36
00:02:15,630 --> 00:02:19,890
The input function looks pretty much identical to the print function,
37
00:02:20,280 --> 00:02:21,990
but instead of the word print,
38
00:02:22,080 --> 00:02:26,220
it's just got the word input. And inside the parentheses,
39
00:02:26,460 --> 00:02:29,460
instead of adding what text will be printed,
40
00:02:29,760 --> 00:02:34,620
we're adding the prompt for the user to give them a hint as to what kind of data
41
00:02:34,620 --> 00:02:38,970
we want. And then when you run this code, it will print out the prompt,
42
00:02:39,240 --> 00:02:43,860
but then there will be a cursor. In some places you'll see it like a flashing
43
00:02:43,860 --> 00:02:45,390
cursor, other places,
44
00:02:45,420 --> 00:02:50,420
it'll just be a solid cursor ready for the user to type in some piece of data.
45
00:02:51,630 --> 00:02:54,960
So what can we do with this data? Well,
46
00:02:54,960 --> 00:02:57,450
we can use it inside our code.
47
00:02:58,140 --> 00:03:03,140
Remember how I said once this line of code executes and I've entered Angela as
48
00:03:03,700 --> 00:03:05,200
the response to this input,
49
00:03:05,650 --> 00:03:10,420
then this part of the code gets replaced by whatever it is that the user typed
50
00:03:10,420 --> 00:03:13,000
in. So in this case it becomes Angela.
51
00:03:13,840 --> 00:03:18,840
And what I can do with it is let's say I wanted to print something like,
52
00:03:19,450 --> 00:03:23,470
hello Angela. Well,
53
00:03:23,470 --> 00:03:28,470
then I can simply write Hello + input and then I'm going to close it off
54
00:03:32,440 --> 00:03:34,120
with another set of brackets.
55
00:03:34,990 --> 00:03:37,990
So I'm going to change my layout instead of side by side,
56
00:03:38,020 --> 00:03:39,880
I'm going to change it to stacked.
57
00:03:40,360 --> 00:03:43,870
And this means that I've got my code at the top and my console at the bottom.
58
00:03:44,230 --> 00:03:47,530
This way my line can go a little bit longer without it rapping.
59
00:03:48,190 --> 00:03:49,750
Notice what's happening here.
60
00:03:50,800 --> 00:03:55,800
This is the input function that I showed you earlier on and this is a print
61
00:03:56,020 --> 00:03:56,853
statement.
62
00:03:56,890 --> 00:04:01,890
So we've got this nested inside the other. And now when I run my code,
63
00:04:03,940 --> 00:04:07,240
what happens is it asked me what is your name?
64
00:04:07,420 --> 00:04:10,660
So I'll write my name and then when I hit enter,
65
00:04:11,230 --> 00:04:16,230
this part is replaced by Angela and it gets concatenated to the word hello and
66
00:04:18,790 --> 00:04:21,130
the entire thing gets printed out down here.
67
00:04:21,880 --> 00:04:25,990
If you find this line of code difficult to understand and to wrap your head
68
00:04:25,990 --> 00:04:26,823
around,
69
00:04:26,890 --> 00:04:31,890
then I recommend heading over to a website called thonny.org and downloading an
70
00:04:31,990 --> 00:04:35,980
application called Thonny. It's completely free and it's available for Windows,
71
00:04:35,980 --> 00:04:40,180
Mac, and Linux. And once you've installed this application,
72
00:04:40,540 --> 00:04:45,540
you can go ahead and paste your line of code here and click on this little debug
73
00:04:46,570 --> 00:04:49,750
symbol. And then we can click on this button,
74
00:04:49,840 --> 00:04:54,760
which is called the step into button to step into the execution of this line of
75
00:04:54,760 --> 00:04:55,420
code
76
00:04:55,420 --> 00:05:00,420
to see how the computer is evaluating this code here step by step.
77
00:05:01,090 --> 00:05:04,870
So you'll see the first thing it tries to do is it will try to run this print
78
00:05:04,870 --> 00:05:08,800
statement. It looks inside, these parentheses to see what it needs to print.
79
00:05:09,370 --> 00:05:11,680
So the first thing is this, Hello,
80
00:05:12,100 --> 00:05:17,020
and it turns that into a string and then it looks at the next thing after the
81
00:05:17,020 --> 00:05:19,510
plus sign to see what it should turn this into.
82
00:05:19,930 --> 00:05:24,160
And this of course is the input function. So as I continue stepping into it,
83
00:05:24,520 --> 00:05:26,410
it's actually going to execute it.
84
00:05:26,740 --> 00:05:31,360
So it's going to run this input function and show the prompt, what is your name?
85
00:05:32,050 --> 00:05:33,760
So that's what shows up down here.
86
00:05:34,300 --> 00:05:37,600
And now if I enter a value in here and hit enter,
87
00:05:38,020 --> 00:05:43,020
then that value that I put right there replaces that previous input function
88
00:05:44,800 --> 00:05:47,200
and is now held inside the print statement.
89
00:05:47,740 --> 00:05:52,740
So if I continue stepping through this code then you'll see it concatenates all of
90
00:05:53,320 --> 00:05:57,590
the pieces together, and finally it just ends up with a simple print
91
00:05:57,680 --> 00:06:01,760
Hello Angela! And if I continue stepping into it,
92
00:06:01,820 --> 00:06:06,620
you'll see that line of code executed until there are no more instructions left.
93
00:06:07,160 --> 00:06:10,040
So if you prefer seeing your code, um,
94
00:06:10,070 --> 00:06:13,490
run like this step by step so that you can see what's going on,
95
00:06:13,760 --> 00:06:15,500
then I recommend giving this a go.
96
00:06:16,580 --> 00:06:21,110
But because of how easy it is to share these Repl.it code bases so that we're
97
00:06:21,110 --> 00:06:24,950
working from the same place, I recommend to do most of your coding here
98
00:06:25,130 --> 00:06:30,130
and only if you get stuck to take a single line of code and paste it into Thonny
99
00:06:30,740 --> 00:06:35,630
to see how it's executed step-by-step. As you're learning to code,
100
00:06:35,690 --> 00:06:37,880
there's going to be new concepts covered.
101
00:06:38,090 --> 00:06:41,300
There's going to be things that take a little bit of thinking before you can
102
00:06:41,300 --> 00:06:42,230
understand it.
103
00:06:42,740 --> 00:06:47,740
So what a lot of programmers love to do is they like to comment in their code.
104
00:06:48,200 --> 00:06:48,410
Now,
105
00:06:48,410 --> 00:06:52,210
you might've seen this already throughout the coding exercises and the solutions
106
00:06:52,230 --> 00:06:54,740
I provided. But in Python,
107
00:06:55,280 --> 00:07:00,280
if you wanted a line of text to not be considered by the computer at all,
108
00:07:01,010 --> 00:07:06,010
all you have to do is add the hashtag or pound sign in front of your text and
109
00:07:08,030 --> 00:07:10,370
this turns it into a comment,
110
00:07:10,580 --> 00:07:13,730
so something that the computer will completely ignore.
111
00:07:14,180 --> 00:07:17,840
So in here you can write code as much as you like, um,
112
00:07:17,870 --> 00:07:20,840
but it won't be executed and when you click run,
113
00:07:21,050 --> 00:07:23,270
you'll see that this is completely ignored.
114
00:07:23,960 --> 00:07:28,430
What I recommend is whenever you come across a new concept in the course to make
115
00:07:28,430 --> 00:07:33,430
a comment above it so that you can explain to yourself what's actually going on
116
00:07:34,370 --> 00:07:37,610
in the line of code below. This way
117
00:07:37,640 --> 00:07:41,630
the next time you come across this line of code and you're not quite sure what's
118
00:07:41,630 --> 00:07:42,710
actually happening here,
119
00:07:43,040 --> 00:07:46,730
you can have a look at the notes that you've written for yourself in your own
120
00:07:46,730 --> 00:07:51,050
words and hopefully it'll re-jog your memory and make it much easier to
121
00:07:51,050 --> 00:07:52,280
comprehend what's going on.
122
00:07:53,720 --> 00:07:58,010
In addition to adding a pound sign manually,
123
00:07:58,160 --> 00:08:03,080
you can also highlight a line of code or simply have your cursor on a line of
124
00:08:03,080 --> 00:08:08,080
code and hold down command and forward slash if you're on Mac or control and
125
00:08:09,290 --> 00:08:11,270
forward slash if you're on Windows.
126
00:08:11,630 --> 00:08:14,240
And if you want to go back or undo your changes,
127
00:08:14,540 --> 00:08:19,160
it's simply command +z or control + z. All right,
128
00:08:19,310 --> 00:08:22,490
so in this lesson we learned about input function,
129
00:08:22,760 --> 00:08:27,680
we learned about putting functions inside other functions and being able to get
130
00:08:27,980 --> 00:08:31,790
user input from the console by using the input function.
131
00:08:32,330 --> 00:08:36,020
So now I've got a coding exercise for you in the next lesson,
132
00:08:36,560 --> 00:08:41,210
and if you've commented this line of code thoroughly and you fully understood
133
00:08:41,420 --> 00:08:45,470
how it works, then you'll be able to breeze through the next exercise.
134
00:08:46,010 --> 00:08:48,650
So for all of that and more, I'll see you there.
13138
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.