Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,120 --> 00:00:01,380
All right. So again,
2
00:00:01,380 --> 00:00:05,400
it's time to put theory into practice and get some hands-on action.
3
00:00:05,760 --> 00:00:10,560
You should be able to find the day 1.3 assignment which is inputs.
4
00:00:11,070 --> 00:00:15,300
Click on it and you will see the instructions for this particular challenge.
5
00:00:15,720 --> 00:00:18,420
And the idea is that you're going to write a program
6
00:00:18,450 --> 00:00:22,950
that's going to print the number of characters in a user's name. For example,
7
00:00:22,950 --> 00:00:25,980
if I input my name, which is Angela,
8
00:00:26,370 --> 00:00:31,370
it should output the number six because there are six characters in the word,
9
00:00:32,189 --> 00:00:36,390
Angela. But remember you can't just print out the number six,
10
00:00:36,450 --> 00:00:38,430
because when I'm checking for this code,
11
00:00:38,700 --> 00:00:42,720
I'm going to try a whole bunch of different names as the input.
12
00:00:43,170 --> 00:00:46,650
And you're going to have to use a little bit of thinking,
13
00:00:47,010 --> 00:00:51,810
a little bit of Googling and also a little bit of messing around with the code
14
00:00:51,840 --> 00:00:54,570
in order to get it to work. If you need any help,
15
00:00:54,600 --> 00:00:58,140
there's a few hints down here for you. And if you really get stuck,
16
00:00:58,350 --> 00:01:00,390
then you can take a look at the solution,
17
00:01:00,450 --> 00:01:03,510
but not until you've really given it a go. And I really,
18
00:01:03,510 --> 00:01:05,400
really want to know that you've tried hard.
19
00:01:06,330 --> 00:01:09,720
And the whole point of these exercises is you sit there and scratch your head a
20
00:01:09,720 --> 00:01:12,420
little bit and actually give it a good go.
21
00:01:13,410 --> 00:01:17,940
So I'm going to stop talking now and I'm going to let you pause the video and
22
00:01:17,940 --> 00:01:20,160
try to complete this challenge. All right, good luck.
23
00:01:23,610 --> 00:01:27,720
Okay. So how did that go? If you get stuck, I want to tell you
24
00:01:27,990 --> 00:01:32,160
the programmers secret to getting unstuck
25
00:01:32,760 --> 00:01:37,170
which is go and grab yourself a cup of tea, have a quick break,
26
00:01:37,410 --> 00:01:39,240
look at something else for a little moment,
27
00:01:39,480 --> 00:01:42,990
think about something else and then come back to it. Well,
28
00:01:42,990 --> 00:01:46,140
the tea part is optional. Well, at least if you're outside of England,
29
00:01:46,170 --> 00:01:50,640
it's optional. So let's get started solving this challenge.
30
00:01:50,820 --> 00:01:54,810
So the idea is that we're going to create a program that counts the number of
31
00:01:54,810 --> 00:01:57,540
characters in any user's name.
32
00:01:58,050 --> 00:02:02,280
And that name is obviously going to come through an input function,
33
00:02:02,730 --> 00:02:07,350
and then we're going to use a function that calculates the length of a string
34
00:02:07,680 --> 00:02:10,770
and we're going to send that out as the output.
35
00:02:11,850 --> 00:02:15,600
The first thing we'll probably need is some form of input function right?
36
00:02:16,080 --> 00:02:20,460
And the prompt that we're going to give the user is what is your name or
37
00:02:20,490 --> 00:02:23,040
something similar that asks them for their name.
38
00:02:23,520 --> 00:02:28,050
And I like to add a space at the end of the prompt. This way, when you run it,
39
00:02:28,230 --> 00:02:33,180
you don't actually have that cursor straight next to the, um, the question mark
40
00:02:33,180 --> 00:02:37,980
which looks a bit weird. But either way, once we've entered an input,
41
00:02:38,340 --> 00:02:42,390
then this part gets replaced by that input
42
00:02:42,660 --> 00:02:43,950
and we're going to do something with it.
43
00:02:43,980 --> 00:02:47,220
We're going to calculate the length of that string.
44
00:02:47,700 --> 00:02:52,230
Now using our eyes, we can obviously see that it's six characters long.
45
00:02:52,560 --> 00:02:55,770
But what if you have a really a long name?
46
00:02:56,700 --> 00:03:00,940
So let's go ahead and take a look at how we might figure out,
47
00:03:01,300 --> 00:03:05,590
um, how to you get a function that calculates the length of a string.
48
00:03:06,100 --> 00:03:09,520
I've actually included the Google search that you might need to use.
49
00:03:10,300 --> 00:03:13,090
And so let's just paste that URL in here,
50
00:03:13,180 --> 00:03:17,740
and you can see that we're searching for how to get the length of the string in
51
00:03:17,800 --> 00:03:21,400
Python and we're trying to get results from Stack Overflow.
52
00:03:21,820 --> 00:03:26,820
The first result is a question that seems to pretty match match exactly what we
53
00:03:27,040 --> 00:03:31,810
want. And the answer from user 225312
54
00:03:31,810 --> 00:03:36,810
tells us that you need to use the Len function. And the way that you use it
55
00:03:37,630 --> 00:03:41,170
is by passing the string inside the parentheses.
56
00:03:41,740 --> 00:03:45,730
Now this part might look a little bit different from what you're used to with the
57
00:03:45,730 --> 00:03:46,930
equal sign and everything.
58
00:03:47,230 --> 00:03:50,140
But I have confidence that if you mess around with this code,
59
00:03:50,350 --> 00:03:51,790
you'll be able to figure it out.
60
00:03:52,840 --> 00:03:56,320
Just like what we did with print or input,
61
00:03:56,620 --> 00:04:01,620
the Len function also has a set of parentheses and it will work its magic on
62
00:04:03,010 --> 00:04:07,480
whatever it is that we put inside the parentheses. If I just put my name,
63
00:04:07,540 --> 00:04:08,373
Angela here,
64
00:04:08,440 --> 00:04:13,440
let's just comment out the previous line, and I go ahead and print the result of
65
00:04:14,260 --> 00:04:18,700
this and then I hit run, you can see that it prints 6. Now,
66
00:04:18,700 --> 00:04:22,330
if I change that to Jack, then you'll see it prints 4.
67
00:04:22,480 --> 00:04:24,970
So it will print whatever it is
68
00:04:25,000 --> 00:04:29,680
the length of the string that I've put inside the Len function.
69
00:04:30,220 --> 00:04:31,900
So we're getting close now, right?
70
00:04:32,110 --> 00:04:36,160
Because instead of printing out a string that I've hard coded here,
71
00:04:36,550 --> 00:04:40,540
what I want is to take the input that the user has given me.
72
00:04:40,960 --> 00:04:44,500
So something like this. Now, if you want to,
73
00:04:44,500 --> 00:04:49,390
you can actually add some spaces inside here just to make it a little bit
74
00:04:49,390 --> 00:04:54,390
clearer to yourself as to what's actually going on because there's three
75
00:04:54,610 --> 00:04:57,490
functions here. They are all nested inside each other.
76
00:04:57,910 --> 00:04:59,680
So this is the first one,
77
00:04:59,680 --> 00:05:03,430
this is the one that is going to get the input from the user and replace this
78
00:05:03,430 --> 00:05:06,250
part with whatever it is that they type down here.
79
00:05:06,910 --> 00:05:11,910
Then that string gets calculated using the Len function to get the length of
80
00:05:12,610 --> 00:05:16,990
that string. And then that number will get printed by the print function.
81
00:05:18,190 --> 00:05:21,640
Now let's go ahead and hit run and let's give it a go.
82
00:05:21,730 --> 00:05:24,550
So let's try Jack, hit enter.
83
00:05:24,790 --> 00:05:27,130
And it tells us that it's 4.
84
00:05:27,700 --> 00:05:29,830
But using the same line of code,
85
00:05:29,950 --> 00:05:34,180
if we try a different name Angela, then it prints out six.
86
00:05:34,930 --> 00:05:37,960
How did you get on with this? If you got stuck,
87
00:05:38,110 --> 00:05:42,160
then have a look at the solution Repl.it um,
88
00:05:42,190 --> 00:05:46,480
where you will be able to find the code that I've written here and a little bit
89
00:05:46,480 --> 00:05:47,890
of explanation as well.
90
00:05:48,730 --> 00:05:51,790
The important thing is that you really play around with this code.
91
00:05:52,150 --> 00:05:57,150
Try things and make it break and make it work and just get it to do what you
92
00:05:58,610 --> 00:05:59,443
want it to do.
93
00:06:00,020 --> 00:06:05,020
And remember that if you ever want to visualize these things and the order of
94
00:06:06,200 --> 00:06:10,700
execution, then you can always use Thonny to see that.
95
00:06:10,880 --> 00:06:12,620
So let's go ahead and step in.
96
00:06:13,010 --> 00:06:17,420
You'll see that it goes through Len, input, and then it's going to carry out this
97
00:06:17,450 --> 00:06:21,650
functionality input. I gave it my name, hit enter.
98
00:06:22,250 --> 00:06:25,580
Then it puts that inside the Len function like so,
99
00:06:25,880 --> 00:06:28,040
so now the next step is this part.
100
00:06:28,280 --> 00:06:30,590
We're going to calculate the length of this string.
101
00:06:31,100 --> 00:06:34,490
And as I continue stepping into it, you can see that become six.
102
00:06:34,820 --> 00:06:37,430
And finally we're gonna carry out the final instruction,
103
00:06:37,460 --> 00:06:41,870
which is just to print six down here. Again,
104
00:06:42,050 --> 00:06:45,890
this can be quite useful just to visualize all the steps that are happening
105
00:06:46,460 --> 00:06:47,293
if you want to.
106
00:06:48,260 --> 00:06:52,490
Once you're done, head over to the next lesson and I'm going to talk to you
107
00:06:52,730 --> 00:06:57,730
all about Python variables and how we'll be able to identify pieces of data in
108
00:06:58,070 --> 00:07:01,520
our code. So for all of that, and more, I'll see you there.
10302
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.