Instructor: Welcome back.
Let's do a fun little exercise.
We just got hired by a gaming company
and we're creating this wizard game.
And this wizard game has the,
is magician in the user's profile.
And for now, we'll just set it to false.
We also have is expert
and whether this user is an expert at this game,
we'll leave it at true for now.
And then, hmm, let's do this here.
I want you to check if both
or check if magician and expert
and if that's the case, if true,
then I want you to print
you are a master magician.
And also I want you to check if magician but not expert.
If that's the case, I want to just print
at least you're getting there.
And then finally, let's do a check that says
if you're not a magician, I want you to say,
well you need magic powers.
Now I want you to use what you've learned
about logical operators to create this conditional logic.
You can pause the video if you want
and try it yourself in a repel.
Otherwise, I'm gonna keep going with an answer.
But you should be able to solve this fairly easily
if you've been following the videos and exercises so far.
So let's start with the first one,
where we say you are a master magician.
So I'm going to say if is expert and is magician,
I am going to print, you are a master magician.
So I'm going to copy that and say,
print you are a master magician.
Nothing too crazy here.
And then next one I'm going to say if,
or I could even say elif if I want, and again,
this is something that you can decide what you prefer.
I usually like using elif if it's part
of the same concept as the first one.
In this case, yeah, we're, we're still trying
to figure out what type of user it is so we'll use elif
and we'll say elif, we wanna check if it's a magician,
so that is is magician.
So we're going to check if that's true,
but we also wanna check if, well not an expert.
Now how do we do this?
I mean we could say use short circuiting and say is expert.
And even if this is false, we can get true here
because is a magician, short circuit and print this part.
At least you're getting there.
Because the interesting here is
that this code block is never going to run
if both of these are true.
Because in the first if block, we're checking that,
so that as soon as this is true
we're going to completely ignore the elif.
So we could do that, but, hmm,
this is a little bit hard to read
because I really have to think about it.
If I come back to the code
or somebody else looks in my code,
it's not clear that unless I comment
that I'm checking if it's magician but not expert.
Because this will never run if is magician is false,
then that's what they're checking for.
I know it's kind of confusing even saying it.
So I prefer using something like this,
using and not,
and that reads a lot better, right?
It makes a lot more sense.
Hey, if this person is an expert and magician, do this.
Otherwise, if this person is magician
and not an expert, then print this.
That reads pretty nicely.
And yes, you can do this because remember,
not as simply checking
and inverting whatever the bullion expression is.
So this still evaluates to a bullion that and can check.
All right, let's finish the final one.
If you're not a magician, you need magic powers.
So how do we check here?
Well, we can do another elif and say,
not is magician.
And if I run it like this
and say print and we'll just copy here,
you need magic powers.
And let's remove the comments for now
just so we can see the blocks of code
and we're gonna test our program.
Hopefully we coded it properly.
Hopefully there's no bugs, but let's have a look.
If I run this, you need magic powers.
Why is that?
Well, if magician is false, none of these are going to work.
What if I change this to true and I click run?
All right, I am a master magician
because I'm an expert as well as a magician,
What if I turn this to false and I click run?
Hey, at least you're getting there.
I'm a magician, but I'm not an expert yet.
I'm still, I still need to improve my gameplay.
Now you may have gotten a different answer than me
and there's many ways to do this, right?
You could have used an elif statement,
you could have used or.
Conditional logic is all about writing code
that makes sense not only to you
but for other people reading your code.
The idea is not to be extremely clever
or really worry about short circuiting.
Yes, it can be more performant, but a machine is so powerful
that just tiny bit of optimization there is usually
not worth it when you're losing perhaps readability.
So you wanna make sure that your code reads like English.
And this is a topic that, frankly,
a lot of beginner programmers have a hard time understanding
where they tried to be as clever as possible,
write as little code as possible
in the shortest possible lines
when instead you wanna focus on readability.
Because a code that is easy to understand
is a sign of a good developer.
And I hope looking at this, you're thinking, yeah,
this is pretty easy.
I mean, this all makes sense, but this is not the only way.
Maybe my version of what reads well is different than yours.
The idea is to each of us
try our best to make our code simple and nice.
And in programming there is no one right answer.
There are many ways to solve a problem
but the key is to solve it
in a simple manner that is readable.
I hope you have fun and I'll see you in the next one.