Instructor: Hi guys and welcome back.
In this video we're going to look at some If statements
with the in keyboard and also a few more examples
so you get a bit more experience.
Here we've got our movies watched which is a set of strings
with the movie titles and then we're asking the user
for a movie they've watched recently.
We can then say user movie in movies watched
and we can put that in an If statement.
So again when Python reaches line four it's going
to evaluate this Boolean and it's gonna say
is the thing the user entered one of the elements
of this collection, this set?
And if it's true then it's going to evaluate this code here
and remember we need indentation in front of this
so that Python can tell what code
is inside the If statement.
So we're gonna print something like
I've watched user movie too.
Otherwise we can print I haven't watched that yet.
If we run this then you can enter something like Green Book
and it'll tell you that I've watched Green Book too.
Of course if you enter something else
then it'll tell you that I haven't watched that yet.
So this is how you can use the in keyword
in a If statement and it just worked in exactly the same way
as it did in the last video when we were printing it out
but now you can use it there in order to make decisions
and potentially do different things in your programme.
Let's work on a slightly different example.
We're going to create now a magic number App.
So in this application we're going to decide
on a magic number, a hidden number from the user.
We're going to ask the user to enter a number
and then we are going to tell the user
whether they got the number right or not.
But before we do that we're going to ask the user
whether they want to play the game or not.
So we'll start by creating our magic number
we'll say number equal seven.
Then we're gonna ask the user whether they want to play.
So we'll say user input equal input enter Y
if you would like play.
Now we're gonna say if user input equal equal Y,
then we will ask them for a number.
So we can do something like user number int of input,
Guess our Number let's say.
So we will ask them to guess our number,
we will get the input that they type
and then we will turn that into an integer
and finally we can use another If statement here
and say user number equal equal number
then we will print that you guessed correctly.
Otherwise we will print sorry it's wrong.
So we're not using the in keyword here at all
but there are a few things that I wanted to tell you
about before we do that.
The first one is that all of these lines are inside
this If statement.
So they will only ever potentially run if this is true.
So if the user enters lowercase Y
then we will start running this line number five,
then we will move on to six and so on.
Number six here is another If statement,
so if this is true then we will run this line
and now you can see that this line has two sets
of indentation because it is both inside this If statement
and this one so therefore we have four eight spaces in total
in front of it so the Python can tell that
this one belongs inside this If statement.
Then we've got an else paired
with this if statement in line six,
which means that if the user number
was not exactly correct given our magic number.
We're going to print sorry it's wrong
and again notice the indentation in this line.
We can run this code and then we'll ask the user
to enter why if they would like to play,
so we are gonna enter Y and then we can guess the number,
let's say seven and you can see
that we guessed it correctly.
Now let's start using the in keyword
because the first thing that you may see users do
is that they may think that because it is a computer
or something they have to enter uppercase Y.
It is quite common in computing to ask the user
for uppercase letters so they might do that
and here you can see that we don't support
this uppercase variant.
So there are two things you can do.
The first one that we've already looked at
is you can turn this into lowercase
and that is actually the better choice of the two.
So I would do this I would turn the users input
into lowercase and then
I would compare the lowercase user input
with the lowercase character that I wanna check.
But in some situations
you may want different options potentially
or you may not wanna do that exactly.
So there is something you can do in this If statement
which is to replace the Equal Equal for In
and then you can have here a tuple of the two letters
that you're willing to accept as your input.
So what we do here is we create a tuple
with two strings, lowercase Y and uppercase Y
and then we're checking whether the users input
is one of those two.
And if it is then we will proceed.
So this is something you could do if you were interested
I'm just showing you that there but I would actually prefer
to turn the users input into the lowercase.
The second place where we could use the in keyword
is if we wanted to give the user a hint regarding how close
or how far they were from our magic number.
So let's say that we wanna tell the user
that they are off by one, if they enter six or eight.
Here in the If statement
after user number equal equal number,
we're going to say elif number minus user number,
in one and minus one we're gonna print you were off by one.
So what this is doing is once again it's defining a tuple
of two numbers that we're willing to accept,
one and minus one.
Then we'll go into subtract from our number
which is seven the user number.
let's say six as an example.
Seven minus six is going to give us one
and then it's going to check whether one
is in this tuple which has one and minus one.
If it is then we're gonna print that you are off by one.
If they enter eight then we will do seven minus eight
which is minus one and minus one
will also be in this tuple.
So we will also print you were off by one.
This is something you could do and potentially
this can be really handy if you want a longer set of values
but for this specific situation there's also a better ways
of doing it without the in keyword
which is by using absolute values.
So you can do ABS,
of number minus user number
and what ABS does is it gives you the absolute value
of this argument.
So if you pass it the value one ABS of one
will give you one but if you pass it minus one,
ABS of minus one will give you one.
It will turn it back into positive.
So now number minus user number will always give you,
how far away the user was from our magic number
in positive terms.
So you can go back to say an equal equal one.
Because this number here will always be one, two, three
and so on.
So if they were off by one then we can just print that.
And I think this is a slightly better approach
than using the in keyword but again the in keyword can
be useful in a wide number of scenarios
where this may not be enough.
If we run this again you'll see that we can type Y
to play then we can guess the number let's say eight
and then it says that you were off by one.
Feel free to play around with this code and change it
and do more things with it if you'd like.
This is a great exercise and will be good practise for you
if you're starting to learn to code.
Thanks for joining me in this video
I hope it's been useful and I'll see you in the next one.