Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,180 --> 00:00:00,810
All right, now
1
2
00:00:00,810 --> 00:00:05,280
it's time to put what you've learned into practice by clicking on the data types
2
3
00:00:05,400 --> 00:00:08,040
day 2.1 code challenge.
3
4
00:00:09,540 --> 00:00:10,470
In this challenge,
4
5
00:00:10,500 --> 00:00:15,450
the idea is that you are going to be given a random two-digit number,
5
6
00:00:15,480 --> 00:00:20,480
so something like 39, and your code that you'll write
6
7
00:00:21,060 --> 00:00:26,060
will be able to add the first digit to the second digit and end up with a
7
8
00:00:26,850 --> 00:00:31,650
numerical result. So for example, if the input was 39,
8
9
00:00:31,680 --> 00:00:36,000
then 3 + 9 would be equal to 12. So the output would be 12.
9
10
00:00:36,480 --> 00:00:39,060
And if the input was 26,
10
11
00:00:39,180 --> 00:00:42,420
then your code needs to print 8 to the console.
11
12
00:00:43,080 --> 00:00:47,100
So you're going to need to think about type checking, type conversion,
12
13
00:00:47,430 --> 00:00:49,710
as well as everything that you learned before.
13
14
00:00:50,550 --> 00:00:54,270
And then you're going to write your code below this line without changing any of
14
15
00:00:54,270 --> 00:00:58,140
these lines. So pause the video now and give this code challenge a go.
15
16
00:01:02,610 --> 00:01:07,200
All right. So how did that go? Did you manage to do it? If not,
16
17
00:01:07,380 --> 00:01:09,900
have a cup of tea or give yourself a little bit of a break,
17
18
00:01:09,900 --> 00:01:12,750
think about something else and then come back to it and try it again.
18
19
00:01:13,530 --> 00:01:14,110
If you've already
19
20
00:01:14,110 --> 00:01:17,340
given it a good go and you want to see what the solution might be,
20
21
00:01:17,700 --> 00:01:18,900
then follow along with me.
21
22
00:01:19,650 --> 00:01:23,250
So the first thing we know is that we're going to have a variable
22
23
00:01:23,280 --> 00:01:26,970
that's going to be equal to a two-digit number.
23
24
00:01:27,540 --> 00:01:30,660
But remember that that could mean anything. That could mean an integer,
24
25
00:01:30,660 --> 00:01:34,590
that could mean a floating-point number, that could even mean a number as a
25
26
00:01:34,590 --> 00:01:35,423
string.
26
27
00:01:35,790 --> 00:01:40,790
So it might be helpful to actually find out the data type of this two-digit
27
28
00:01:41,310 --> 00:01:46,200
number that we get. And to do that, we of course use the type function, all
28
29
00:01:46,200 --> 00:01:48,810
lowercase. And then in between the parentheses,
29
30
00:01:48,840 --> 00:01:52,470
we add the thing that we want to check. So it's this variable.
30
31
00:01:53,040 --> 00:01:56,130
And then let's go ahead and print the result into the console.
31
32
00:01:56,550 --> 00:02:00,390
So now let's run our code and let's type a two-digit number,
32
33
00:02:00,570 --> 00:02:01,590
say 56.
33
34
00:02:02,100 --> 00:02:07,100
and we get the result that the type of this two-digit number is a string
34
35
00:02:08,670 --> 00:02:13,410
because, of course, we're getting hold of this through the input function and the
35
36
00:02:13,410 --> 00:02:17,280
user could be typing anything into this input, a string,
36
37
00:02:17,310 --> 00:02:18,840
their name or a number.
37
38
00:02:19,290 --> 00:02:22,560
So now that we know that this variable has the datatype string,
38
39
00:02:22,950 --> 00:02:27,090
then you can think of a little bit about what we know we can do with strings.
39
40
00:02:27,330 --> 00:02:31,920
We can subscript strings, right? So if we had a string like hello,
40
41
00:02:32,250 --> 00:02:36,330
and we want it to get a hold of the first character, we could put zero there.
41
42
00:02:36,570 --> 00:02:37,860
And when I print this,
42
43
00:02:38,160 --> 00:02:42,120
we'll get H being selected out of this string.
43
44
00:02:43,050 --> 00:02:48,050
So if we know that we can subscript strings to get a hold of a particular value
44
45
00:02:50,130 --> 00:02:54,690
at a particular position just by using subscripting like this,
45
46
00:02:54,990 --> 00:02:58,020
then we can do the same with our two-digit number.
46
47
00:02:58,230 --> 00:03:02,950
So we could say two_digit_number, let's get hold of the first digit.
47
48
00:03:03,130 --> 00:03:04,030
So let's call it
48
49
00:03:04,030 --> 00:03:08,830
first_digit equals two_digit_number [
49
50
00:03:08,860 --> 00:03:09,693
0],
50
51
00:03:10,120 --> 00:03:15,120
and then the second_digit could be two_digit_number [1].
51
52
00:03:16,660 --> 00:03:17,493
Now,
52
53
00:03:17,680 --> 00:03:22,180
when I go ahead and print these two items,
53
54
00:03:22,210 --> 00:03:25,990
first_digit and second_digit,
54
55
00:03:26,440 --> 00:03:30,280
then you'll see that if I enter a number like 87,
55
56
00:03:30,760 --> 00:03:34,600
then my first digit is 8 and my second digit is 7.
56
57
00:03:35,110 --> 00:03:39,820
So now I can probably start thinking about adding those two digits together.
57
58
00:03:40,420 --> 00:03:40,990
For example,
58
59
00:03:40,990 --> 00:03:45,990
we can say that result equals first_digit plus second_digit.
59
60
00:03:47,200 --> 00:03:51,790
But of course, when I run this right now and I print the value of result,
60
61
00:03:52,270 --> 00:03:54,910
you'll see that for a number like 87
61
62
00:03:55,390 --> 00:04:00,340
what I will get is just a eight concatenated to seven,
62
63
00:04:00,520 --> 00:04:04,030
which turns it back into 87, which is not what I want at all.
63
64
00:04:04,390 --> 00:04:07,840
Especially after all of my hard work separating out the digits.
64
65
00:04:08,470 --> 00:04:11,770
What I want to do instead is to turn each of these,
65
66
00:04:12,070 --> 00:04:15,010
which currently has the data type of string,
66
67
00:04:15,190 --> 00:04:20,190
so I can demonstrate that to you by saying type of first_digit,
67
68
00:04:20,950 --> 00:04:24,460
you can see it is a string.
68
69
00:04:25,000 --> 00:04:30,000
So if I could only change first_digit and second_digit from string data types
69
70
00:04:31,000 --> 00:04:36,000
into an integer data type by using the type conversion that we learned about
70
71
00:04:37,240 --> 00:04:41,320
in the last lesson, well then once I do that,
71
72
00:04:41,350 --> 00:04:46,120
then this plus sign has a whole different meaning because now it's going to add
72
73
00:04:46,150 --> 00:04:50,260
this first_digit to the second_digit instead of concatenating it.
73
74
00:04:50,620 --> 00:04:53,350
So now if I go ahead and print the results,
74
75
00:04:55,420 --> 00:04:58,270
then you'll see that for the same number
75
76
00:04:59,770 --> 00:05:02,500
87, my result is now 15.
76
77
00:05:03,310 --> 00:05:08,140
I've now managed to achieve what the code exercise wants me to do.
77
78
00:05:09,560 --> 00:05:12,190
So did you manage to get it right? If not,
78
79
00:05:12,220 --> 00:05:15,340
go ahead and have a look at the model solution
79
80
00:05:15,610 --> 00:05:19,990
and I've written some comments for each line of code that walks you through each
80
81
00:05:19,990 --> 00:05:20,823
of the steps.
81
82
00:05:21,070 --> 00:05:25,930
Now notice that instead of converting the second_digit here,
82
83
00:05:26,380 --> 00:05:30,250
you can also do it up here, like what you see right here.
83
84
00:05:31,270 --> 00:05:32,950
So I have a play around with the code,
84
85
00:05:33,010 --> 00:05:36,520
especially if you struggled with this and didn't manage to get it right.
85
86
00:05:37,060 --> 00:05:40,660
And then once you've understood what's going on here line by line,
86
87
00:05:41,080 --> 00:05:43,300
if you need, have a look at the previous videos,
87
88
00:05:43,600 --> 00:05:46,390
then go back to the code challenge and attempt it again.
88
89
00:05:46,960 --> 00:05:50,260
And only once you're happy that you really understand everything that was
89
90
00:05:50,260 --> 00:05:51,610
covered in this challenge,
90
91
00:05:51,880 --> 00:05:54,910
then head over to the next lesson where we're going to talk more about
91
92
00:05:54,940 --> 00:05:56,290
mathematical operators
92
93
00:05:56,560 --> 00:06:01,540
like the plus sign and others that you can use in your code. So for all of that
93
94
00:06:01,660 --> 00:06:03,490
and more, I'll see you on the next lesson.
8907
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.