WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:03.374 [MUSIC PLAYING] 00:00:05.310 --> 00:00:09.340 SPEAKER: Well, hello, one and all, and welcome to our short on conditionals. 00:00:09.340 --> 00:00:12.540 We'll see here how we can use these things called conditionals 00:00:12.540 --> 00:00:16.079 to ask questions in our program and have our program take 00:00:16.079 --> 00:00:19.690 different paths depending on the answers to those questions. 00:00:19.690 --> 00:00:23.170 Now, I have here a program called recommendations.py. 00:00:23.170 --> 00:00:26.550 And the goal of this program is to be able to recommend some card 00:00:26.550 --> 00:00:30.120 games to a user based on their preferences for difficulty 00:00:30.120 --> 00:00:32.740 and the number of players they want to play with. 00:00:32.740 --> 00:00:37.530 So it seems like our program should take multiple paths, recommending to a user 00:00:37.530 --> 00:00:41.100 the right card game based on any given input the user might 00:00:41.100 --> 00:00:42.908 give to our program. 00:00:42.908 --> 00:00:44.700 Well, the first thing we should probably do 00:00:44.700 --> 00:00:47.970 is ask the user a question themselves to get data 00:00:47.970 --> 00:00:51.460 on how they're feeling about what kind of card game they want to play. 00:00:51.460 --> 00:00:55.170 So I'll first ask them a question using the Python input 00:00:55.170 --> 00:00:57.070 function to get some user input. 00:00:57.070 --> 00:01:00.300 And as we've seen before, I can give as input to input 00:01:00.300 --> 00:01:03.870 a string that will ask, let's say, a question to the user. 00:01:03.870 --> 00:01:09.170 Maybe the first question is, do you prefer difficult or casual card games? 00:01:09.170 --> 00:01:10.910 So difficult or casual? 00:01:10.910 --> 00:01:14.455 And why don't we store the result of whatever the user typed in, 00:01:14.455 --> 00:01:19.090 in a variable, maybe this one called difficulty, just like this. 00:01:19.090 --> 00:01:21.970 Now, another question would be, well, how many players 00:01:21.970 --> 00:01:23.120 do you want to play with? 00:01:23.120 --> 00:01:26.510 Do you want maybe multiplayer, as in more than one player? 00:01:26.510 --> 00:01:29.920 Or do you want a single-player game? 00:01:29.920 --> 00:01:32.420 Single-player, just like this. 00:01:32.420 --> 00:01:37.700 And why don't we, just for consistency, capitalize these just like that? 00:01:37.700 --> 00:01:41.470 And now if I store this in the players variable, 00:01:41.470 --> 00:01:45.010 I'm able now to see that I have two variables storing 00:01:45.010 --> 00:01:47.800 both the difficulty the user wants the players 00:01:47.800 --> 00:01:49.840 that they might want to play with. 00:01:49.840 --> 00:01:54.610 Now, the question then becomes, how do we recommend a game based 00:01:54.610 --> 00:01:56.920 on the input the users gave us? 00:01:56.920 --> 00:02:00.050 Well, we have down here a function called recommend. 00:02:00.050 --> 00:02:02.830 I can simply pass as input to recommend a game. 00:02:02.830 --> 00:02:05.830 And we'll see in our terminal, "You might like," space, 00:02:05.830 --> 00:02:07.750 followed by the game name itself. 00:02:07.750 --> 00:02:09.770 But that doesn't help us really up here. 00:02:09.770 --> 00:02:11.770 We can certainly call recommend. 00:02:11.770 --> 00:02:13.550 But how can we depend-- 00:02:13.550 --> 00:02:18.650 how can we make sure the game actually depends on the input the user gave us? 00:02:18.650 --> 00:02:20.740 Well, for that, we can use a conditional. 00:02:20.740 --> 00:02:26.230 And in Python, to make a conditional, we have this keyword called if. 00:02:26.230 --> 00:02:29.630 And if is followed by something called a Boolean expression-- 00:02:29.630 --> 00:02:30.790 more on those later-- 00:02:30.790 --> 00:02:35.450 but essentially, a question we can ask that has a yes or no response. 00:02:35.450 --> 00:02:37.930 Now, maybe the first question I could ask 00:02:37.930 --> 00:02:45.110 is, is the difficulty here equal to, in this case, difficult? 00:02:45.110 --> 00:02:49.330 That is, did the user type in "Difficult," capital D? 00:02:49.330 --> 00:02:53.380 I'll go ahead and make sure this goes on a new block in my code, 00:02:53.380 --> 00:02:54.590 just for style's sake. 00:02:54.590 --> 00:02:58.060 And for now, why don't I leave this branch of code as just 00:02:58.060 --> 00:03:01.840 dot, dot, dot, meaning I'll get back to it a little bit later. 00:03:01.840 --> 00:03:04.190 So this is our first conditional. 00:03:04.190 --> 00:03:07.870 We're asking the question, did the player type in, capital D, "Difficult" 00:03:07.870 --> 00:03:09.370 for difficulty? 00:03:09.370 --> 00:03:12.500 And then whatever code is indented will run 00:03:12.500 --> 00:03:18.100 if this condition, this Boolean expression, is true. 00:03:18.100 --> 00:03:19.970 So what else could we do? 00:03:19.970 --> 00:03:23.840 Well, presumably, the user might not always type in "Difficult." 00:03:23.840 --> 00:03:26.000 They could type in "Casual," for instance. 00:03:26.000 --> 00:03:29.530 Well, in that case, we could actually specify what should happen in the case 00:03:29.530 --> 00:03:32.060 that this condition is not true. 00:03:32.060 --> 00:03:37.820 Notice how on line 6, indented here, we have a branch in our code that will run 00:03:37.820 --> 00:03:42.940 if this condition is true, difficulty is equal to, capital D, "Difficult." 00:03:42.940 --> 00:03:45.460 But otherwise, else, if that's not the case, 00:03:45.460 --> 00:03:50.920 we'll run whatever code is now indented on line 8 and beyond, if we'd like. 00:03:50.920 --> 00:03:53.980 So a good first question to ask, but now I 00:03:53.980 --> 00:03:57.650 think we should ask another question too, about number of players as well. 00:03:57.650 --> 00:04:01.900 So if, let's say, the player typed in difficulty "Difficult," 00:04:01.900 --> 00:04:03.900 but then we might also want to ask them, well, 00:04:03.900 --> 00:04:05.650 how many players do you want to play with? 00:04:05.650 --> 00:04:07.750 That could inform our recommendation. 00:04:07.750 --> 00:04:10.730 Within this branch, I could also ask another question. 00:04:10.730 --> 00:04:17.140 I could say, if players is equal to maybe multiplayer, 00:04:17.140 --> 00:04:22.040 dot dot, dot, then we could probably recommend some given game, in this case. 00:04:22.040 --> 00:04:25.960 But of course, similar to difficulty, a player or a user could actually type 00:04:25.960 --> 00:04:28.012 in something different than "Multiplayer." 00:04:28.012 --> 00:04:29.470 They could type in "Single-player." 00:04:29.470 --> 00:04:32.170 So we could make use of else here as well-- 00:04:32.170 --> 00:04:33.950 else, dot, dot, dot. 00:04:33.950 --> 00:04:37.690 So now we're building up the branches of this program. 00:04:37.690 --> 00:04:41.470 Within this branch of difficult games, we'll ask the player, 00:04:41.470 --> 00:04:43.970 do they prefer multiplayer or single-player. 00:04:43.970 --> 00:04:45.860 And if multiplayer, we'll take this path. 00:04:45.860 --> 00:04:48.610 If single-player, we'll take this path. 00:04:48.610 --> 00:04:52.120 Let's do the same thing down below here, because even if the player specifies 00:04:52.120 --> 00:04:56.590 casual, well, we still want to make an informed decision of whether they wanted 00:04:56.590 --> 00:04:59.180 multiplayer or single-player games too. 00:04:59.180 --> 00:05:04.230 So I'll go ahead and ask the same question-- if players == multiplayer, 00:05:04.230 --> 00:05:05.380 just like this. 00:05:05.380 --> 00:05:07.420 We'll have one branch of our code. 00:05:07.420 --> 00:05:10.360 Else we'll have another branch just like this. 00:05:10.360 --> 00:05:14.940 And notice now there are four branches, four places we could end up with, 00:05:14.940 --> 00:05:16.500 which makes sense. 00:05:16.500 --> 00:05:21.240 If we have given the player two options, difficulty and players, 00:05:21.240 --> 00:05:24.180 each of which has two possible inputs, well, there 00:05:24.180 --> 00:05:27.430 are four possible games we could recommend. 00:05:27.430 --> 00:05:31.410 So if a player wants a difficult multiplayer card game, 00:05:31.410 --> 00:05:33.270 maybe recommend to them poker, for instance. 00:05:33.270 --> 00:05:36.090 I'll recommend poker. 00:05:36.090 --> 00:05:39.570 If they want a difficult single-player game, 00:05:39.570 --> 00:05:41.730 we could perhaps recommend to them maybe a game 00:05:41.730 --> 00:05:45.790 called klondike, which is the classic solitaire game, if you're familiar. 00:05:45.790 --> 00:05:48.330 But down below, let's see down here, if they 00:05:48.330 --> 00:05:52.560 want a casual game that is multiplayer, we could recommend maybe a game 00:05:52.560 --> 00:05:53.920 like hearts. 00:05:53.920 --> 00:05:57.420 And if they want a casual, maybe, single-player game, 00:05:57.420 --> 00:05:59.370 we could recommend one-- 00:05:59.370 --> 00:06:02.320 maybe one like clock, which is really just luck based. 00:06:02.320 --> 00:06:05.170 So here now, we have four games to recommend 00:06:05.170 --> 00:06:07.550 based on the input the user gave us. 00:06:07.550 --> 00:06:09.830 And this is our conditional structure. 00:06:09.830 --> 00:06:11.680 We've used conditionals to actually modify 00:06:11.680 --> 00:06:14.920 how our program runs, having it take different pathways depending 00:06:14.920 --> 00:06:17.030 on the input the user gave us. 00:06:17.030 --> 00:06:18.200 So let's try it. 00:06:18.200 --> 00:06:25.780 If I were to, in this case, run python of recommendations.py and enter 00:06:25.780 --> 00:06:27.190 "Difficult"-- 00:06:27.190 --> 00:06:30.440 let's say "Difficult"-- and "Multiplayer," 00:06:30.440 --> 00:06:32.480 which game do you think we should get? 00:06:32.480 --> 00:06:34.630 Well, probably in this case poker. 00:06:34.630 --> 00:06:36.560 Difficulty was "Difficult." 00:06:36.560 --> 00:06:38.830 So we'll be sat inside of this branch here. 00:06:38.830 --> 00:06:41.680 Now, players is now going to be "Multiplayer." 00:06:41.680 --> 00:06:44.000 So we'll end up at this branch here. 00:06:44.000 --> 00:06:45.040 I'll hit Enter. 00:06:45.040 --> 00:06:47.380 And we'll see "You might like Poker." 00:06:47.380 --> 00:06:48.940 So pretty good. 00:06:48.940 --> 00:06:52.250 But what could go wrong in this case? 00:06:52.250 --> 00:06:57.770 So maybe the user is a little more malicious than we think they might be. 00:06:57.770 --> 00:07:00.830 Maybe they don't want even a casual game. 00:07:00.830 --> 00:07:03.950 They want something that's like a medium difficulty as well. 00:07:03.950 --> 00:07:05.560 So I'll type in "Medium." 00:07:05.560 --> 00:07:10.300 And maybe they want not just a multiplayer or a single-player game. 00:07:10.300 --> 00:07:12.980 They actually want a two-player game. 00:07:12.980 --> 00:07:17.110 And notice how these inputs are not explicitly talked 00:07:17.110 --> 00:07:18.830 about inside of our conditional. 00:07:18.830 --> 00:07:20.480 So I'm curious where we'll end up. 00:07:20.480 --> 00:07:24.700 If I hit Enter here, we'll see "You might like Clock." 00:07:24.700 --> 00:07:27.440 Now, clock is definitively a single-player game. 00:07:27.440 --> 00:07:29.870 So we should not have ended up at clock here. 00:07:29.870 --> 00:07:32.380 But there is a reason that we did. 00:07:32.380 --> 00:07:37.300 So we asked the question here, if difficulty is equal to, capital D, 00:07:37.300 --> 00:07:40.130 "Difficult," then we'll do this branch here. 00:07:40.130 --> 00:07:44.710 If that's not the case, though, we'll be in this branch down below here. 00:07:44.710 --> 00:07:48.670 Now, there are many other options besides just, capital D, "Difficult" 00:07:48.670 --> 00:07:49.760 a user could type in. 00:07:49.760 --> 00:07:51.740 In this case, I typed in "Medium." 00:07:51.740 --> 00:07:54.160 And because that was not equal to "Difficult," well, 00:07:54.160 --> 00:07:56.590 we're going to end up in this branch down here. 00:07:56.590 --> 00:08:02.060 And similarly, I typed in "Two-player," which is definitely not "Multiplayer." 00:08:02.060 --> 00:08:05.030 So we then ended up at this particular branch here. 00:08:05.030 --> 00:08:09.140 So else is good when there are no other options you want to test for. 00:08:09.140 --> 00:08:12.730 But in this case, I'd argue we want to test if the user actually typed 00:08:12.730 --> 00:08:17.103 in, in this case, "Casual" or "Single-player" explicitly. 00:08:17.103 --> 00:08:19.520 So let's see how we could do that with these conditionals. 00:08:19.520 --> 00:08:23.620 Well, it turns out there's one more keyword beyond if and else, 00:08:23.620 --> 00:08:27.610 one called elif, which is kind of an elision, if you will, 00:08:27.610 --> 00:08:30.850 a collision of both "else" and "if." 00:08:30.850 --> 00:08:37.210 And it's a way of asking some question if the prior condition was not true. 00:08:37.210 --> 00:08:41.080 So here, I did ask if difficulty is equal to "Difficult," capital D. 00:08:41.080 --> 00:08:46.570 But otherwise, if it's not, elif, we could ask some other question as well. 00:08:46.570 --> 00:08:51.910 Maybe I'll ask if difficulty was equal to, in this case, "Casual," 00:08:51.910 --> 00:08:56.200 just like this, with a capital C. And that opens up for me some new branch 00:08:56.200 --> 00:08:58.840 in my conditional structure. 00:08:58.840 --> 00:09:03.940 Now, it seems to me that I actually want to move this conditional structure 00:09:03.940 --> 00:09:08.120 inside the branch that explicitly asks for casual games. 00:09:08.120 --> 00:09:11.720 So I'll go ahead and copy and paste this and move it inside this branch. 00:09:11.720 --> 00:09:16.360 And now what should happen if we end up at this else branch here? 00:09:16.360 --> 00:09:18.790 Well, we know the player didn't type in "Difficult." 00:09:18.790 --> 00:09:21.040 We know they didn't type in "Casual." 00:09:21.040 --> 00:09:23.540 So maybe we could print something else entirely. 00:09:23.540 --> 00:09:27.520 We could say maybe "Enter a valid difficulty," 00:09:27.520 --> 00:09:31.690 letting them know that we don't really know how to recommend games that are not 00:09:31.690 --> 00:09:35.230 either difficult or casual, in this case. 00:09:35.230 --> 00:09:37.100 Now, what else can we do inside here? 00:09:37.100 --> 00:09:40.690 I think there's still the same problem with these two conditional structures 00:09:40.690 --> 00:09:41.300 here. 00:09:41.300 --> 00:09:43.310 Let's make use of elif here as well. 00:09:43.310 --> 00:09:49.070 I'll say, elif players == "Single-player," just like this. 00:09:49.070 --> 00:09:52.930 That opens up a new branch that I can then move "Klondike" into. 00:09:52.930 --> 00:09:59.860 And then down below, why don't I print maybe "Enter a valid number of players," 00:09:59.860 --> 00:10:01.270 just like this. 00:10:01.270 --> 00:10:02.900 I'll do the same thing down below. 00:10:02.900 --> 00:10:08.400 In fact, I can copy and paste this, for now, down to this branch here, 00:10:08.400 --> 00:10:09.893 our "Casual" branch. 00:10:09.893 --> 00:10:12.810 And now, we want to make sure we actually change these recommendations 00:10:12.810 --> 00:10:15.970 to "Hearts" and "Clock." 00:10:15.970 --> 00:10:19.500 And I think that should be set for us. 00:10:19.500 --> 00:10:21.010 So what have we done? 00:10:21.010 --> 00:10:23.280 We've made it able for us to actually check 00:10:23.280 --> 00:10:26.290 if the user has entered a value different from what we expected. 00:10:26.290 --> 00:10:29.290 We're now more flexible and less trusting of the user. 00:10:29.290 --> 00:10:30.790 So let's try this. 00:10:30.790 --> 00:10:36.030 I'll go ahead and I will run python of recommendations.py. 00:10:36.030 --> 00:10:36.900 I'll hit Enter. 00:10:36.900 --> 00:10:42.250 And I'll type in, let's say, a "Casual" game, maybe "Multiplayer." 00:10:42.250 --> 00:10:45.660 And that should put us at hearts, let's say. 00:10:45.660 --> 00:10:46.600 I see hearts. 00:10:46.600 --> 00:10:47.278 That's good. 00:10:47.278 --> 00:10:48.820 Now let's be a little more malicious. 00:10:48.820 --> 00:10:55.440 If I type in something like "Medium" and maybe "Two-player," as well, 00:10:55.440 --> 00:10:57.180 where do you think we'll end up? 00:10:57.180 --> 00:11:00.880 Well, it seems like we should end up in "Enter a valid difficulty." 00:11:00.880 --> 00:11:01.480 So we'll see. 00:11:01.480 --> 00:11:02.340 Hit Enter. 00:11:02.340 --> 00:11:05.960 And we will see "Enter a valid difficulty." 00:11:05.960 --> 00:11:08.960 So I argue that this program should work for us. 00:11:08.960 --> 00:11:13.210 If we were to enter in all combinations of "Difficult," "Single-player," 00:11:13.210 --> 00:11:16.960 "Multiplayer," or "Casual," we'd get the right game that we would want to be 00:11:16.960 --> 00:11:19.090 recommending, in this case. 00:11:19.090 --> 00:11:21.920 Now, there are still some improvements to be made. 00:11:21.920 --> 00:11:24.770 Notice one here is this idea of copying and pasting. 00:11:24.770 --> 00:11:26.410 We can probably do better than that. 00:11:26.410 --> 00:11:29.740 But for that, we need to learn more about this idea of a Boolean 00:11:29.740 --> 00:11:30.800 expressions. 00:11:30.800 --> 00:11:33.330 More on that another time.