[MUSIC PLAYING]
SPEAKER: Well, hello, one and all, and welcome to our short on conditionals.
We'll see here how we can use these things called conditionals
to ask questions in our program and have our program take
different paths depending on the answers to those questions.
Now, I have here a program called recommendations.py.
And the goal of this program is to be able to recommend some card
games to a user based on their preferences for difficulty
and the number of players they want to play with.
So it seems like our program should take multiple paths, recommending to a user
the right card game based on any given input the user might
give to our program.
Well, the first thing we should probably do
is ask the user a question themselves to get data
on how they're feeling about what kind of card game they want to play.
So I'll first ask them a question using the Python input
function to get some user input.
And as we've seen before, I can give as input to input
a string that will ask, let's say, a question to the user.
Maybe the first question is, do you prefer difficult or casual card games?
So difficult or casual?
And why don't we store the result of whatever the user typed in,
in a variable, maybe this one called difficulty, just like this.
Now, another question would be, well, how many players
do you want to play with?
Do you want maybe multiplayer, as in more than one player?
Or do you want a single-player game?
Single-player, just like this.
And why don't we, just for consistency, capitalize these just like that?
And now if I store this in the players variable,
I'm able now to see that I have two variables storing
both the difficulty the user wants the players
that they might want to play with.
Now, the question then becomes, how do we recommend a game based
on the input the users gave us?
Well, we have down here a function called recommend.
I can simply pass as input to recommend a game.
And we'll see in our terminal, "You might like," space,
followed by the game name itself.
But that doesn't help us really up here.
We can certainly call recommend.
But how can we depend--
how can we make sure the game actually depends on the input the user gave us?
Well, for that, we can use a conditional.
And in Python, to make a conditional, we have this keyword called if.
And if is followed by something called a Boolean expression--
more on those later--
but essentially, a question we can ask that has a yes or no response.
Now, maybe the first question I could ask
is, is the difficulty here equal to, in this case, difficult?
That is, did the user type in "Difficult," capital D?
I'll go ahead and make sure this goes on a new block in my code,
just for style's sake.
And for now, why don't I leave this branch of code as just
dot, dot, dot, meaning I'll get back to it a little bit later.
So this is our first conditional.
We're asking the question, did the player type in, capital D, "Difficult"
for difficulty?
And then whatever code is indented will run
if this condition, this Boolean expression, is true.
So what else could we do?
Well, presumably, the user might not always type in "Difficult."
They could type in "Casual," for instance.
Well, in that case, we could actually specify what should happen in the case
that this condition is not true.
Notice how on line 6, indented here, we have a branch in our code that will run
if this condition is true, difficulty is equal to, capital D, "Difficult."
But otherwise, else, if that's not the case,
we'll run whatever code is now indented on line 8 and beyond, if we'd like.
So a good first question to ask, but now I
think we should ask another question too, about number of players as well.
So if, let's say, the player typed in difficulty "Difficult,"
but then we might also want to ask them, well,
how many players do you want to play with?
That could inform our recommendation.
Within this branch, I could also ask another question.
I could say, if players is equal to maybe multiplayer,
dot dot, dot, then we could probably recommend some given game, in this case.
But of course, similar to difficulty, a player or a user could actually type
in something different than "Multiplayer."
They could type in "Single-player."
So we could make use of else here as well--
else, dot, dot, dot.
So now we're building up the branches of this program.
Within this branch of difficult games, we'll ask the player,
do they prefer multiplayer or single-player.
And if multiplayer, we'll take this path.
If single-player, we'll take this path.
Let's do the same thing down below here, because even if the player specifies
casual, well, we still want to make an informed decision of whether they wanted
multiplayer or single-player games too.
So I'll go ahead and ask the same question-- if players == multiplayer,
just like this.
We'll have one branch of our code.
Else we'll have another branch just like this.
And notice now there are four branches, four places we could end up with,
which makes sense.
If we have given the player two options, difficulty and players,
each of which has two possible inputs, well, there
are four possible games we could recommend.
So if a player wants a difficult multiplayer card game,
maybe recommend to them poker, for instance.
I'll recommend poker.
If they want a difficult single-player game,
we could perhaps recommend to them maybe a game
called klondike, which is the classic solitaire game, if you're familiar.
But down below, let's see down here, if they
want a casual game that is multiplayer, we could recommend maybe a game
like hearts.
And if they want a casual, maybe, single-player game,
we could recommend one--
maybe one like clock, which is really just luck based.
So here now, we have four games to recommend
based on the input the user gave us.
And this is our conditional structure.
We've used conditionals to actually modify
how our program runs, having it take different pathways depending
on the input the user gave us.
So let's try it.
If I were to, in this case, run python of recommendations.py and enter
"Difficult"--
let's say "Difficult"-- and "Multiplayer,"
which game do you think we should get?
Well, probably in this case poker.
Difficulty was "Difficult."
So we'll be sat inside of this branch here.
Now, players is now going to be "Multiplayer."
So we'll end up at this branch here.
I'll hit Enter.
And we'll see "You might like Poker."
So pretty good.
But what could go wrong in this case?
So maybe the user is a little more malicious than we think they might be.
Maybe they don't want even a casual game.
They want something that's like a medium difficulty as well.
So I'll type in "Medium."
And maybe they want not just a multiplayer or a single-player game.
They actually want a two-player game.
And notice how these inputs are not explicitly talked
about inside of our conditional.
So I'm curious where we'll end up.
If I hit Enter here, we'll see "You might like Clock."
Now, clock is definitively a single-player game.
So we should not have ended up at clock here.
But there is a reason that we did.
So we asked the question here, if difficulty is equal to, capital D,
"Difficult," then we'll do this branch here.
If that's not the case, though, we'll be in this branch down below here.
Now, there are many other options besides just, capital D, "Difficult"
a user could type in.
In this case, I typed in "Medium."
And because that was not equal to "Difficult," well,
we're going to end up in this branch down here.
And similarly, I typed in "Two-player," which is definitely not "Multiplayer."
So we then ended up at this particular branch here.
So else is good when there are no other options you want to test for.
But in this case, I'd argue we want to test if the user actually typed
in, in this case, "Casual" or "Single-player" explicitly.
So let's see how we could do that with these conditionals.
Well, it turns out there's one more keyword beyond if and else,
one called elif, which is kind of an elision, if you will,
a collision of both "else" and "if."
And it's a way of asking some question if the prior condition was not true.
So here, I did ask if difficulty is equal to "Difficult," capital D.
But otherwise, if it's not, elif, we could ask some other question as well.
Maybe I'll ask if difficulty was equal to, in this case, "Casual,"
just like this, with a capital C. And that opens up for me some new branch
in my conditional structure.
Now, it seems to me that I actually want to move this conditional structure
inside the branch that explicitly asks for casual games.
So I'll go ahead and copy and paste this and move it inside this branch.
And now what should happen if we end up at this else branch here?
Well, we know the player didn't type in "Difficult."
We know they didn't type in "Casual."
So maybe we could print something else entirely.
We could say maybe "Enter a valid difficulty,"
letting them know that we don't really know how to recommend games that are not
either difficult or casual, in this case.
Now, what else can we do inside here?
I think there's still the same problem with these two conditional structures
here.
Let's make use of elif here as well.
I'll say, elif players == "Single-player," just like this.
That opens up a new branch that I can then move "Klondike" into.
And then down below, why don't I print maybe "Enter a valid number of players,"
just like this.
I'll do the same thing down below.
In fact, I can copy and paste this, for now, down to this branch here,
our "Casual" branch.
And now, we want to make sure we actually change these recommendations
to "Hearts" and "Clock."
And I think that should be set for us.
So what have we done?
We've made it able for us to actually check
if the user has entered a value different from what we expected.
We're now more flexible and less trusting of the user.
So let's try this.
I'll go ahead and I will run python of recommendations.py.
I'll hit Enter.
And I'll type in, let's say, a "Casual" game, maybe "Multiplayer."
And that should put us at hearts, let's say.
I see hearts.
That's good.
Now let's be a little more malicious.
If I type in something like "Medium" and maybe "Two-player," as well,
where do you think we'll end up?
Well, it seems like we should end up in "Enter a valid difficulty."
So we'll see.
Hit Enter.
And we will see "Enter a valid difficulty."
So I argue that this program should work for us.
If we were to enter in all combinations of "Difficult," "Single-player,"
"Multiplayer," or "Casual," we'd get the right game that we would want to be
recommending, in this case.
Now, there are still some improvements to be made.
Notice one here is this idea of copying and pasting.
We can probably do better than that.
But for that, we need to learn more about this idea of a Boolean
expressions.
More on that another time.