Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
All right guys, it's time to show what you're made of.
2
Here's another code challenge. And this one is again,
3
another difficult challenge and the arms really show you just how difficult it
4
is, but you're going to flex and you're going to get stronger.
5
And it's only through struggle that you're going to become a better coder.
6
So in this code exercise, we're going to build a love calculator.
7
And the way that it's going to work is the same method that I used when I was
8
10, pretty much.
9
And I really recommend reading up on this particular Buzzfeed article,
10
which is of course, very reputable
11
and it's going to tell you exactly how this program is going to work.
12
So once you've managed to scroll past all the GIFs, it will tell you, um,
13
the method. So you write your name, his or her name,
14
and then the words true and love. Now,
15
then you are going to count the number of times T-R-U-E occur in both of
16
your names. And then you're going to count the number of times,
17
L-O-V-E occur in both of your names. Once you've got those numbers,
18
so here T-R-U-E adds up to six, L-O-V-E adds up to three.
19
Then you'll get the percentage that you are compatible with this other person.
20
So not only did you get to learn Programming on this course,
21
you also get the discover your destiny.
22
The idea is that we're going to convert this very time-consuming process into a
23
computer program. So once you've understood how it works,
24
you're going to write the code for it. Now,
25
in order to be able to calculate the love score,
26
there's two important things that you should read in the hint.
27
One is you will need to use the lower function, and all that this does is it
28
takes a string, let's say we've got the string Angela.
29
And notice how that first letter is a capital A.
30
If I write .lower parentheses,
31
then it will reduce all of the capital letters to lowercase letters.
32
And if you were wondering how you would've known to use this,
33
then you can head over to the Stack of flow question
34
in which somebody asks exactly this question, how do you turn something
35
that's uppercase into a lowercase character in Python.
36
The second function you'll need is the count function.
37
And this will give you the number of times a letter occurs in a string,
38
which is super useful when it comes to this particular challenge. So again,
39
let's use our string, Angela, and then I'm going to write count.
40
And then in between the parentheses,
41
I'm going to specify what it is that I want to count.
42
So let's say I want to count the character A inside this string,
43
and I hit enter. It comes up with one,
44
even though you can see there's actually two A's in the string, in my name,
45
it only counts one lowercase a. So that's why this first step was super,
46
super useful.
47
So let's say lower_case_name equals, um,
48
Angela.lower. And then,
49
um,
50
let's go ahead and do lower_case_name.count,
51
and then we're going to count for a again. Now you'll see it gives us two,
52
because we were able to convert everything to lowercase,
53
and then use the count function to check the number of times the character a
54
occurs in this string. And again, I've linked to a Stack Overflow question
55
which is how you might've found out to use this count function and how you might
56
use it in terms of code examples. Now,
57
there's a second step to this challenge
58
because not only do we want to know the love score,
59
we also want to give the user an interpretation.
60
So the interpretation is here. For love scores that are less than 10 or greater
61
than 90, the message is going to say "Your score is X." And of course,
62
replace that X with the actual score. You go together like Coke and Mentos.
63
Now, if their love score was between 40 and 50,
64
the message is going to read your score is, whatever it may be,
65
you are all right together. And finally,
66
for all other scores, the message is just going to give them their score.
67
Your score is XYZ. In this case, there's two inputs,
68
somebody's name, and then somebody else's name.
69
And then you're going to check for the number of times T-R-U-E occurs in
70
both of those names, L-O-V-E occurs in those names,
71
then you going to combine these digits together to get a love score.
72
Pause the video now, have a look at everything that's in the instructions,
73
read it all and then go ahead and give that a go.
74
[inaudible]
75
All right. So the first step is to work out the love score, right?
76
So we need to figure out how many times each of these letters occur in these
77
names.
78
The first thing that we need to do is to combine the names,
79
because we're looking for the number of times,
80
let's say a T occurs in these names and we're treating them as one string,
81
right? So I don't wanna check it separately and then have to add it together.
82
So let's call it a combined_string and lets set that to equal name1 + name2.
83
And this is just going to concatenate those two strings together
84
so we'll be able to check them only once. It makes it more efficient,
85
right? The next step is to guard against cases
86
where the user is inputting their name like this, which many people do,
87
or they might even write it like this.
88
So we have to turn everything into lowercase.
89
So let's call it a lower_case_string
90
and that will be combined_string. lower just as I demoed previously using
91
this lower function. So now we've got everything in lower case,
92
we're going to start checking for this first word,
93
T-R-U-E. So let's first check
94
how many times the letter T occurs in this lowercase string of both people's
95
names.
96
So we can say lower_case_string.count
97
and then inside the parentheses, we specify the string we're looking for
98
which is 't' and then I'm going to save this to a variable
99
which I'll just call 't' again. And then we're going to repeat this several times,
100
hopefully for practice sake, you'll type it all out. But in my case,
101
I'm just going to copy and paste it, which is usually error-prone,
102
but we'll try to be careful.
103
So now that we've got the number of times 't' occurs, 'r'
104
occurs, 'u' and 'e' occurs, we can now add it all up.
105
So let's say true = t + r + u + e.
106
So now we've got the first digit of our love score and we have to go ahead and
107
do the same thing for L-O-V-E. So again, for time sake,
108
I'm just going to copy that and then go ahead and change this to l,
109
o, v, e and l, o, v, e.
110
And then I'm going to go ahead and calculate the total for that
111
which is going to be l + o + v + e.
112
And now I've got both of these numbers and I'm going to try and concatenate them
113
together to achieve this two-digit number.
114
To do that, I'm going to create a love score variable
115
that's going to keep hold of this.
116
And then I'm going to say true + love.
117
And remember that at this stage, both true and love are actually integers.
118
That's how we were able to add them together.
119
So if I want to turn them into a string in order to be able to concatenate them
120
together, I have to convert them to strings.
121
The final thing we have to do is just to go ahead and print this love score.
122
So we're not going to print anything else for now.
123
Let's just go ahead and test it out. So let's run our code
124
and let's type my name and then
125
their name, Jack Bauer, of course. And then it gives us the score.
126
So five, three.
127
So that means everything so far has pretty much worked.
128
So now all we have to do is use our knowledge of if statements to give a
129
corresponding interpretation or a message.
130
If the score is less than 10 or greater than 90,
131
so if love_score is less than 10 or love_score is greater than 90,
132
so we're using that logical or operator here to check if either this condition
133
or this condition is true. And if you find it helpful,
134
which I usually do,
135
it's easy to wrap these conditions in their own set of parentheses.
136
Now, Python doesn't actually care. Either method works.
137
But for us humans, this is a little bit more readable.
138
So under this condition,
139
what we're going to print out is
140
Your love score is,
141
and then we'll insert the love_score variable here,
142
and we're going to of course be using f-strings, so let's add that.
143
And then we're going to write you go together like Coke and Mentos.
144
So that's our first condition done. Now,
145
the next condition is what if their love score is between 40 and 50.
146
So let's go ahead and add an elif here.
147
Elif
148
the love_score is greater than or equal to 40
149
and the love_score is less than or equal to 50,
150
so that catches the cases where the love score is between 40 and 50.
151
So both of these conditions must be true for this statement to fire.
152
And in this case,
153
what we want to do is we want to print out this sentence.
154
So your score is,
155
let's just insert the love_score in here.
156
[inaudible]
157
Your score is this particular love score. You are alright together.
158
Now the final thing we said is if it doesn't match any of these conditions,
159
then the message is just going to be their score.
160
So that means we can catch everything else using an else statement
161
and we can simply just print "Your score
162
is this particular number."
163
Now at this stage, you might think you're done, but if you actually run it,
164
you're going to get an error. So let's go ahead and test it with the same names
165
as before.
166
You can see that we get a type error on line 27,
167
and it says that the comparison less than is not supported between instances
168
of string and int.
169
So the problem here is that in order to concatenate these two numbers,
170
we had to turn them into strings.
171
So that let's say that this number was equal to five and this number was equal
172
to six. Then instead of getting 11, which is not what we want,
173
we actually get 56 'cause they're both strings and these strings get concatenated
174
together.
175
But the problem occurs on the next step because we wanna compare the love score
176
against an actual number, then if it's a string,
177
it's not going to work. So in order to solve this,
178
then we actually have to convert the love score into an integer.
179
So you could use a separate step.
180
So the int_score equals int
181
and then we wrap it around the love score. Alternatively, um,
182
and a lot easier is to simply just wrap that whole line inside a set of brackets
183
and convert the whole thing into an integer on the same line.
184
It depends on how comfortable you are with this code as to which option you'll
185
choose. Now that we're done here,
186
let's go ahead and run our code again and let's just check it out.
187
Your score is 42. You are all right together.
188
So now our code seems to be working and we've now completed this code challenge.
189
How did you get on with this challenge?
190
Did you manage to get everything right?
191
Did you get stuck at certain places?
192
Have a think about which parts of this challenge were the hardest and see if you
193
can review some of those concepts so that the next time you encounter them,
194
you'll be able to tackle them with confidence.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.