Are you looking to find true love?
Well, this coding exercise will be able to help you
with just that.
This challenge is to create a love calculator.
You're going to write a program that tests the compatibility
between two people.
It doesn't really matter if you believe this or not,
we're going to use it to test out our programming skills.
There are many ways of working out the love-score
between two people,
and if you've ever been to high school,
you might have encountered one of these methods.
This method that I've put into this coding exercise comes
from the well-known and well-established, respectable source
of a Buzzfeed article.
And essentially, it works like this.
So you take both people's names and you check
for the number of times the letters in the word true occurs.
So, T-R-U-E
and then you check for the number of times letters
in the word love occurs, so L-O-V-E, and then,
you combine these two numbers to make a two digit number
and that is your love score percentage.
What we're going to do is we're also
going to give people an interpretation.
So if their love score is less than 10 or greater than 90,
then we're going to send them the message
saying, "Your score is *x*
you go together like coke and mentos."
And then for people with a love score between 40 and 50,
which is kind of on the fence,
we say, "Your love score is *y*, you are all right together.
And then finally, if none of those conditions are true
and people are sort of somewhere
between 20 and 40 and 50 and 90,
then we'll just tell them, "Your score is..." whatever it is.
If you take a look in the description box,
you'll see an example of how we've worked out
name1, which is my name and name2,
which is Jack Bauer.
And you can see that T-R-U-E occurs a total
of 5 times in both of our names,
and L-O-V-E occurs a total of 3 times
in both of our names
and so the final love score is 53, which is neither here
nor there, which is not fantastic, but hey,
it is what it is.
And so that means our program
is just simply going to print out the score.
"Your score is 53."
You can take a look at some other examples that I've got
in here to check against,
and also I've got some hints in there
and some scores for different names.
This is a difficult challenge, and as I said before,
if you get stuck on logic, try writing out
your own flow chart program
before you actually go and commit it to code,
otherwise, just try things, test things, see if it works
and you can try out the different variations
at the bottom of the description pane, Brad Pitt,
Jennifer Aniston, Prince William,
Kate Middleton, et cetera, and make sure that your output
matches the score that's shown in that table.
And this is just to help you make sure
that your code is working. And when your code is working,
then also you have to make sure that your output
matches what the description pane wants it to say exactly,
so the interpretations. And I recommend just copy
and pasting some of those interpretations
and substituting the parts appropriately.
So, good luck and enjoy.
All right.
Firstly, we know we get hold
of the name1 and name2 in our variables name1,
name2 on lines two and three.
And what we're going to do
on line five is to combine these two names together
so that we have just a single string to check.
We write combine names equals name1 plus name2.
The next step is to take all of the characters
and make sure their casing is the same
as what we're going to check it against
because the user might have typed in capitalized names,
they might have put the names in all caps,
they could have put it all in lowercase,
we don't want all of that ambiguity.
So we're going to check the names against lowercase letters.
So we're also going to use the ".lower() method"
to make our combined names into lowercase.
That means we take our name1, name2, for example
in the Input, we've got David Beckham, and Victoria Beckham.
By combining it on line five, we now have
a single variable called davidbeckhamvictoriabeckham
and by making it lowercase,
all of those letters are now lowercase letters.
The next step is to calculate the first digit
in the love score,
which according to the rules is to check how many times
the letter T-R-U-E occurs in both the names.
So we're going to check it against our lower names,
the combined lowercase names, and we're checking it
against lowercase letters as well, t-r-u-e.
And then, we use the "count() method" to see how many times
that letter occurs in our combined name.
And we add those numbers all up together in order
to get hold of our first_digit for our love calculator.
And similarly, we do the same thing for the second_digit.
We count how many times the letters l-o-v-e occurs
in the combined lowercase names, and we add all
of those occurrences up to get our second_digit.
Once we've got both digits,
then we can combine them together.
The way that this calculator works is we take
the first_digit and the second_digit
and we create a two-digit number.
So we can't just add it together like maths.
It's not like 9 + 8 equals 17,
it's actually 9 + 8 equals 98.
So that means, we're going to have to turn our digits,
which are numbers into strings
so that we combine them rather than add them up.
To do this, we write the str() method, we wrap it
around the digit that we want to turn into a string
and then we use the plus sign (+)
in order to concatenate it together
so that they occur one after the other as if it was text.
Now, the final step is to check the score
against the conditions that we set out
in the description, so we can give people an interpretation
of their score. Because in this case,
we're going to have to compare numbers against numbers,
then it makes sense to turn our combined love score
into an integer, so we wrap that combined string
into an int() method to turn it
into a number that we can compare against other numbers.
All that's left to do is just to write
an if, elif, else statement
in order to check against the three conditions.
If the score is less than 10 or if it's greater than 90,
then we print the first statement.
Otherwise, if the score is greater than
or equal to 40 and it's less than or equal to 50,
so between 40 and 50, then we print the second statement.
And if all of those are not true, then else, we
just print out their score in the third statement.
That's all there is to this exercise.
If you had any issues, then go back
to the previous slide and see
if you can fix your code to make it pass all the tests.