Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,030 --> 00:00:02,310
All right guys, it's time to show what you're made of.
2
00:00:02,370 --> 00:00:05,700
Here's another code challenge. And this one is again,
3
00:00:05,790 --> 00:00:10,560
another difficult challenge and the arms really show you just how difficult it
4
00:00:10,560 --> 00:00:14,040
is, but you're going to flex and you're going to get stronger.
5
00:00:14,580 --> 00:00:17,910
And it's only through struggle that you're going to become a better coder.
6
00:00:18,480 --> 00:00:22,950
So in this code exercise, we're going to build a love calculator.
7
00:00:23,640 --> 00:00:28,640
And the way that it's going to work is the same method that I used when I was
8
00:00:29,010 --> 00:00:30,090
10, pretty much.
9
00:00:30,690 --> 00:00:35,310
And I really recommend reading up on this particular Buzzfeed article,
10
00:00:35,940 --> 00:00:38,730
which is of course, very reputable
11
00:00:38,880 --> 00:00:42,450
and it's going to tell you exactly how this program is going to work.
12
00:00:43,110 --> 00:00:47,490
So once you've managed to scroll past all the GIFs, it will tell you, um,
13
00:00:47,580 --> 00:00:51,210
the method. So you write your name, his or her name,
14
00:00:51,540 --> 00:00:54,480
and then the words true and love. Now,
15
00:00:54,480 --> 00:00:59,480
then you are going to count the number of times T-R-U-E occur in both of
16
00:00:59,520 --> 00:01:02,640
your names. And then you're going to count the number of times,
17
00:01:02,640 --> 00:01:07,560
L-O-V-E occur in both of your names. Once you've got those numbers,
18
00:01:07,590 --> 00:01:12,570
so here T-R-U-E adds up to six, L-O-V-E adds up to three.
19
00:01:12,900 --> 00:01:17,550
Then you'll get the percentage that you are compatible with this other person.
20
00:01:18,060 --> 00:01:20,550
So not only did you get to learn Programming on this course,
21
00:01:20,550 --> 00:01:22,650
you also get the discover your destiny.
22
00:01:23,190 --> 00:01:28,190
The idea is that we're going to convert this very time-consuming process into a
23
00:01:28,500 --> 00:01:32,880
computer program. So once you've understood how it works,
24
00:01:33,150 --> 00:01:35,820
you're going to write the code for it. Now,
25
00:01:35,850 --> 00:01:37,950
in order to be able to calculate the love score,
26
00:01:37,950 --> 00:01:41,340
there's two important things that you should read in the hint.
27
00:01:41,940 --> 00:01:46,940
One is you will need to use the lower function, and all that this does is it
28
00:01:48,360 --> 00:01:51,390
takes a string, let's say we've got the string Angela.
29
00:01:51,780 --> 00:01:54,990
And notice how that first letter is a capital A.
30
00:01:55,680 --> 00:01:58,770
If I write .lower parentheses,
31
00:01:59,220 --> 00:02:04,020
then it will reduce all of the capital letters to lowercase letters.
32
00:02:04,590 --> 00:02:08,220
And if you were wondering how you would've known to use this,
33
00:02:08,580 --> 00:02:11,190
then you can head over to the Stack of flow question
34
00:02:11,670 --> 00:02:15,960
in which somebody asks exactly this question, how do you turn something
35
00:02:15,960 --> 00:02:19,740
that's uppercase into a lowercase character in Python.
36
00:02:20,970 --> 00:02:23,820
The second function you'll need is the count function.
37
00:02:24,150 --> 00:02:28,350
And this will give you the number of times a letter occurs in a string,
38
00:02:28,380 --> 00:02:33,270
which is super useful when it comes to this particular challenge. So again,
39
00:02:33,300 --> 00:02:37,830
let's use our string, Angela, and then I'm going to write count.
40
00:02:38,040 --> 00:02:39,930
And then in between the parentheses,
41
00:02:40,230 --> 00:02:43,350
I'm going to specify what it is that I want to count.
42
00:02:43,740 --> 00:02:48,570
So let's say I want to count the character A inside this string,
43
00:02:48,930 --> 00:02:51,270
and I hit enter. It comes up with one,
44
00:02:51,780 --> 00:02:56,190
even though you can see there's actually two A's in the string, in my name,
45
00:02:56,760 --> 00:03:01,760
it only counts one lowercase a. So that's why this first step was super,
46
00:03:02,320 --> 00:03:03,370
super useful.
47
00:03:03,610 --> 00:03:08,320
So let's say lower_case_name equals, um,
48
00:03:09,250 --> 00:03:13,660
Angela.lower. And then,
49
00:03:13,900 --> 00:03:14,733
um,
50
00:03:14,890 --> 00:03:19,890
let's go ahead and do lower_case_name.count,
51
00:03:21,730 --> 00:03:26,440
and then we're going to count for a again. Now you'll see it gives us two,
52
00:03:26,680 --> 00:03:29,110
because we were able to convert everything to lowercase,
53
00:03:29,260 --> 00:03:33,610
and then use the count function to check the number of times the character a
54
00:03:33,670 --> 00:03:38,080
occurs in this string. And again, I've linked to a Stack Overflow question
55
00:03:38,350 --> 00:03:43,350
which is how you might've found out to use this count function and how you might
56
00:03:43,660 --> 00:03:46,900
use it in terms of code examples. Now,
57
00:03:46,930 --> 00:03:49,120
there's a second step to this challenge
58
00:03:49,120 --> 00:03:52,270
because not only do we want to know the love score,
59
00:03:52,390 --> 00:03:55,450
we also want to give the user an interpretation.
60
00:03:55,930 --> 00:04:00,930
So the interpretation is here. For love scores that are less than 10 or greater
61
00:04:02,020 --> 00:04:06,880
than 90, the message is going to say "Your score is X." And of course,
62
00:04:06,880 --> 00:04:11,770
replace that X with the actual score. You go together like Coke and Mentos.
63
00:04:13,960 --> 00:04:16,960
Now, if their love score was between 40 and 50,
64
00:04:16,990 --> 00:04:20,470
the message is going to read your score is, whatever it may be,
65
00:04:20,950 --> 00:04:24,460
you are all right together. And finally,
66
00:04:24,490 --> 00:04:28,600
for all other scores, the message is just going to give them their score.
67
00:04:28,630 --> 00:04:33,340
Your score is XYZ. In this case, there's two inputs,
68
00:04:33,370 --> 00:04:35,530
somebody's name, and then somebody else's name.
69
00:04:35,890 --> 00:04:39,670
And then you're going to check for the number of times T-R-U-E occurs in
70
00:04:39,670 --> 00:04:43,060
both of those names, L-O-V-E occurs in those names,
71
00:04:43,330 --> 00:04:46,810
then you going to combine these digits together to get a love score.
72
00:04:47,590 --> 00:04:51,670
Pause the video now, have a look at everything that's in the instructions,
73
00:04:51,910 --> 00:04:54,340
read it all and then go ahead and give that a go.
74
00:04:54,660 --> 00:04:59,660
[inaudible]
75
00:05:03,210 --> 00:05:07,950
All right. So the first step is to work out the love score, right?
76
00:05:08,160 --> 00:05:13,160
So we need to figure out how many times each of these letters occur in these
77
00:05:14,250 --> 00:05:15,083
names.
78
00:05:15,390 --> 00:05:20,390
The first thing that we need to do is to combine the names,
79
00:05:21,120 --> 00:05:23,400
because we're looking for the number of times,
80
00:05:23,400 --> 00:05:28,400
let's say a T occurs in these names and we're treating them as one string,
81
00:05:30,030 --> 00:05:33,150
right? So I don't wanna check it separately and then have to add it together.
82
00:05:33,600 --> 00:05:38,600
So let's call it a combined_string and lets set that to equal name1 + name2.
83
00:05:41,080 --> 00:05:45,150
And this is just going to concatenate those two strings together
84
00:05:45,540 --> 00:05:49,440
so we'll be able to check them only once. It makes it more efficient,
85
00:05:49,500 --> 00:05:53,760
right? The next step is to guard against cases
86
00:05:53,790 --> 00:05:57,830
where the user is inputting their name like this, which many people do,
87
00:05:58,160 --> 00:06:00,530
or they might even write it like this.
88
00:06:00,920 --> 00:06:03,980
So we have to turn everything into lowercase.
89
00:06:04,130 --> 00:06:06,080
So let's call it a lower_case_string
90
00:06:09,460 --> 00:06:14,460
and that will be combined_string. lower just as I demoed previously using
91
00:06:15,820 --> 00:06:20,050
this lower function. So now we've got everything in lower case,
92
00:06:20,080 --> 00:06:23,590
we're going to start checking for this first word,
93
00:06:23,980 --> 00:06:27,070
T-R-U-E. So let's first check
94
00:06:27,100 --> 00:06:32,100
how many times the letter T occurs in this lowercase string of both people's
95
00:06:33,070 --> 00:06:33,903
names.
96
00:06:34,030 --> 00:06:38,500
So we can say lower_case_string.count
97
00:06:38,920 --> 00:06:42,820
and then inside the parentheses, we specify the string we're looking for
98
00:06:42,850 --> 00:06:47,350
which is 't' and then I'm going to save this to a variable
99
00:06:47,350 --> 00:06:52,330
which I'll just call 't' again. And then we're going to repeat this several times,
100
00:06:52,810 --> 00:06:56,890
hopefully for practice sake, you'll type it all out. But in my case,
101
00:06:56,950 --> 00:07:01,180
I'm just going to copy and paste it, which is usually error-prone,
102
00:07:01,240 --> 00:07:03,010
but we'll try to be careful.
103
00:07:05,620 --> 00:07:08,830
So now that we've got the number of times 't' occurs, 'r'
104
00:07:08,830 --> 00:07:12,760
occurs, 'u' and 'e' occurs, we can now add it all up.
105
00:07:13,240 --> 00:07:18,240
So let's say true = t + r + u + e.
106
00:07:21,160 --> 00:07:26,160
So now we've got the first digit of our love score and we have to go ahead and
107
00:07:26,800 --> 00:07:31,690
do the same thing for L-O-V-E. So again, for time sake,
108
00:07:31,690 --> 00:07:36,690
I'm just going to copy that and then go ahead and change this to l,
109
00:07:39,100 --> 00:07:44,100
o, v, e and l, o, v, e.
110
00:07:47,050 --> 00:07:51,580
And then I'm going to go ahead and calculate the total for that
111
00:07:51,670 --> 00:07:56,670
which is going to be l + o + v + e.
112
00:07:58,690 --> 00:08:03,690
And now I've got both of these numbers and I'm going to try and concatenate them
113
00:08:04,960 --> 00:08:08,290
together to achieve this two-digit number.
114
00:08:09,340 --> 00:08:12,640
To do that, I'm going to create a love score variable
115
00:08:12,700 --> 00:08:14,170
that's going to keep hold of this.
116
00:08:14,620 --> 00:08:18,130
And then I'm going to say true + love.
117
00:08:18,760 --> 00:08:23,260
And remember that at this stage, both true and love are actually integers.
118
00:08:23,290 --> 00:08:25,510
That's how we were able to add them together.
119
00:08:26,050 --> 00:08:30,310
So if I want to turn them into a string in order to be able to concatenate them
120
00:08:30,310 --> 00:08:33,070
together, I have to convert them to strings.
121
00:08:35,200 --> 00:08:40,200
The final thing we have to do is just to go ahead and print this love score.
122
00:08:40,870 --> 00:08:43,390
So we're not going to print anything else for now.
123
00:08:43,420 --> 00:08:47,440
Let's just go ahead and test it out. So let's run our code
124
00:08:49,630 --> 00:08:52,990
and let's type my name and then
125
00:08:53,080 --> 00:08:57,750
their name, Jack Bauer, of course. And then it gives us the score.
126
00:08:57,750 --> 00:08:59,730
So five, three.
127
00:09:00,480 --> 00:09:03,780
So that means everything so far has pretty much worked.
128
00:09:04,350 --> 00:09:09,350
So now all we have to do is use our knowledge of if statements to give a
129
00:09:09,570 --> 00:09:13,050
corresponding interpretation or a message.
130
00:09:13,740 --> 00:09:17,880
If the score is less than 10 or greater than 90,
131
00:09:18,330 --> 00:09:23,330
so if love_score is less than 10 or love_score is greater than 90,
132
00:09:28,710 --> 00:09:33,710
so we're using that logical or operator here to check if either this condition
133
00:09:35,310 --> 00:09:39,660
or this condition is true. And if you find it helpful,
134
00:09:39,660 --> 00:09:41,610
which I usually do,
135
00:09:41,820 --> 00:09:46,770
it's easy to wrap these conditions in their own set of parentheses.
136
00:09:47,220 --> 00:09:51,000
Now, Python doesn't actually care. Either method works.
137
00:09:51,300 --> 00:09:54,660
But for us humans, this is a little bit more readable.
138
00:09:55,470 --> 00:09:57,630
So under this condition,
139
00:09:57,750 --> 00:10:00,510
what we're going to print out is
140
00:10:02,370 --> 00:10:04,530
Your love score is,
141
00:10:04,770 --> 00:10:07,980
and then we'll insert the love_score variable here,
142
00:10:09,450 --> 00:10:12,900
and we're going to of course be using f-strings, so let's add that.
143
00:10:13,530 --> 00:10:18,530
And then we're going to write you go together like Coke and Mentos.
144
00:10:22,350 --> 00:10:25,530
So that's our first condition done. Now,
145
00:10:25,560 --> 00:10:30,560
the next condition is what if their love score is between 40 and 50.
146
00:10:32,730 --> 00:10:35,550
So let's go ahead and add an elif here.
147
00:10:38,190 --> 00:10:38,970
Elif
148
00:10:38,970 --> 00:10:43,970
the love_score is greater than or equal to 40
149
00:10:47,220 --> 00:10:52,220
and the love_score is less than or equal to 50,
150
00:10:53,130 --> 00:10:58,130
so that catches the cases where the love score is between 40 and 50.
151
00:10:58,440 --> 00:11:03,440
So both of these conditions must be true for this statement to fire.
152
00:11:05,100 --> 00:11:06,060
And in this case,
153
00:11:06,090 --> 00:11:10,770
what we want to do is we want to print out this sentence.
154
00:11:11,670 --> 00:11:13,830
So your score is,
155
00:11:15,420 --> 00:11:18,000
let's just insert the love_score in here.
156
00:11:18,410 --> 00:11:22,790
[inaudible]
157
00:11:23,330 --> 00:11:27,530
Your score is this particular love score. You are alright together.
158
00:11:28,430 --> 00:11:33,430
Now the final thing we said is if it doesn't match any of these conditions,
159
00:11:33,860 --> 00:11:36,980
then the message is just going to be their score.
160
00:11:37,520 --> 00:11:41,900
So that means we can catch everything else using an else statement
161
00:11:42,260 --> 00:11:46,100
and we can simply just print "Your score
162
00:11:46,220 --> 00:11:48,830
is this particular number."
163
00:11:50,210 --> 00:11:54,430
Now at this stage, you might think you're done, but if you actually run it,
164
00:11:54,460 --> 00:11:59,140
you're going to get an error. So let's go ahead and test it with the same names
165
00:11:59,140 --> 00:11:59,973
as before.
166
00:12:02,440 --> 00:12:05,650
You can see that we get a type error on line 27,
167
00:12:06,040 --> 00:12:11,040
and it says that the comparison less than is not supported between instances
168
00:12:12,400 --> 00:12:13,810
of string and int.
169
00:12:14,320 --> 00:12:19,320
So the problem here is that in order to concatenate these two numbers,
170
00:12:19,960 --> 00:12:22,270
we had to turn them into strings.
171
00:12:22,750 --> 00:12:27,700
So that let's say that this number was equal to five and this number was equal
172
00:12:27,700 --> 00:12:31,900
to six. Then instead of getting 11, which is not what we want,
173
00:12:32,290 --> 00:12:37,290
we actually get 56 'cause they're both strings and these strings get concatenated
174
00:12:38,650 --> 00:12:39,483
together.
175
00:12:40,060 --> 00:12:45,060
But the problem occurs on the next step because we wanna compare the love score
176
00:12:45,850 --> 00:12:50,380
against an actual number, then if it's a string,
177
00:12:50,770 --> 00:12:54,670
it's not going to work. So in order to solve this,
178
00:12:55,120 --> 00:12:59,830
then we actually have to convert the love score into an integer.
179
00:13:00,130 --> 00:13:02,200
So you could use a separate step.
180
00:13:02,440 --> 00:13:06,220
So the int_score equals int
181
00:13:06,910 --> 00:13:11,830
and then we wrap it around the love score. Alternatively, um,
182
00:13:11,920 --> 00:13:16,920
and a lot easier is to simply just wrap that whole line inside a set of brackets
183
00:13:19,120 --> 00:13:22,660
and convert the whole thing into an integer on the same line.
184
00:13:23,110 --> 00:13:27,220
It depends on how comfortable you are with this code as to which option you'll
185
00:13:27,220 --> 00:13:29,830
choose. Now that we're done here,
186
00:13:29,830 --> 00:13:33,880
let's go ahead and run our code again and let's just check it out.
187
00:13:38,280 --> 00:13:41,130
Your score is 42. You are all right together.
188
00:13:41,550 --> 00:13:46,550
So now our code seems to be working and we've now completed this code challenge.
189
00:13:47,520 --> 00:13:50,310
How did you get on with this challenge?
190
00:13:50,400 --> 00:13:53,070
Did you manage to get everything right?
191
00:13:53,460 --> 00:13:55,950
Did you get stuck at certain places?
192
00:13:56,850 --> 00:14:01,440
Have a think about which parts of this challenge were the hardest and see if you
193
00:14:01,440 --> 00:14:05,280
can review some of those concepts so that the next time you encounter them,
194
00:14:05,340 --> 00:14:07,410
you'll be able to tackle them with confidence.
18929
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.