1 00:00:00,000 --> 00:00:03,374 [MUSIC PLAYING] 2 00:00:03,374 --> 00:00:05,310 3 00:00:05,310 --> 00:00:09,340 SPEAKER: Well, hello, one and all, and welcome to our short on conditionals. 4 00:00:09,340 --> 00:00:12,540 We'll see here how we can use these things called conditionals 5 00:00:12,540 --> 00:00:16,079 to ask questions in our program and have our program take 6 00:00:16,079 --> 00:00:19,690 different paths depending on the answers to those questions. 7 00:00:19,690 --> 00:00:23,170 Now, I have here a program called recommendations.py. 8 00:00:23,170 --> 00:00:26,550 And the goal of this program is to be able to recommend some card 9 00:00:26,550 --> 00:00:30,120 games to a user based on their preferences for difficulty 10 00:00:30,120 --> 00:00:32,740 and the number of players they want to play with. 11 00:00:32,740 --> 00:00:37,530 So it seems like our program should take multiple paths, recommending to a user 12 00:00:37,530 --> 00:00:41,100 the right card game based on any given input the user might 13 00:00:41,100 --> 00:00:42,908 give to our program. 14 00:00:42,908 --> 00:00:44,700 Well, the first thing we should probably do 15 00:00:44,700 --> 00:00:47,970 is ask the user a question themselves to get data 16 00:00:47,970 --> 00:00:51,460 on how they're feeling about what kind of card game they want to play. 17 00:00:51,460 --> 00:00:55,170 So I'll first ask them a question using the Python input 18 00:00:55,170 --> 00:00:57,070 function to get some user input. 19 00:00:57,070 --> 00:01:00,300 And as we've seen before, I can give as input to input 20 00:01:00,300 --> 00:01:03,870 a string that will ask, let's say, a question to the user. 21 00:01:03,870 --> 00:01:09,170 Maybe the first question is, do you prefer difficult or casual card games? 22 00:01:09,170 --> 00:01:10,910 So difficult or casual? 23 00:01:10,910 --> 00:01:14,455 And why don't we store the result of whatever the user typed in, 24 00:01:14,455 --> 00:01:19,090 in a variable, maybe this one called difficulty, just like this. 25 00:01:19,090 --> 00:01:21,970 Now, another question would be, well, how many players 26 00:01:21,970 --> 00:01:23,120 do you want to play with? 27 00:01:23,120 --> 00:01:26,510 Do you want maybe multiplayer, as in more than one player? 28 00:01:26,510 --> 00:01:29,920 Or do you want a single-player game? 29 00:01:29,920 --> 00:01:32,420 Single-player, just like this. 30 00:01:32,420 --> 00:01:37,700 And why don't we, just for consistency, capitalize these just like that? 31 00:01:37,700 --> 00:01:41,470 And now if I store this in the players variable, 32 00:01:41,470 --> 00:01:45,010 I'm able now to see that I have two variables storing 33 00:01:45,010 --> 00:01:47,800 both the difficulty the user wants the players 34 00:01:47,800 --> 00:01:49,840 that they might want to play with. 35 00:01:49,840 --> 00:01:54,610 Now, the question then becomes, how do we recommend a game based 36 00:01:54,610 --> 00:01:56,920 on the input the users gave us? 37 00:01:56,920 --> 00:02:00,050 Well, we have down here a function called recommend. 38 00:02:00,050 --> 00:02:02,830 I can simply pass as input to recommend a game. 39 00:02:02,830 --> 00:02:05,830 And we'll see in our terminal, "You might like," space, 40 00:02:05,830 --> 00:02:07,750 followed by the game name itself. 41 00:02:07,750 --> 00:02:09,770 But that doesn't help us really up here. 42 00:02:09,770 --> 00:02:11,770 We can certainly call recommend. 43 00:02:11,770 --> 00:02:13,550 But how can we depend-- 44 00:02:13,550 --> 00:02:18,650 how can we make sure the game actually depends on the input the user gave us? 45 00:02:18,650 --> 00:02:20,740 Well, for that, we can use a conditional. 46 00:02:20,740 --> 00:02:26,230 And in Python, to make a conditional, we have this keyword called if. 47 00:02:26,230 --> 00:02:29,630 And if is followed by something called a Boolean expression-- 48 00:02:29,630 --> 00:02:30,790 more on those later-- 49 00:02:30,790 --> 00:02:35,450 but essentially, a question we can ask that has a yes or no response. 50 00:02:35,450 --> 00:02:37,930 Now, maybe the first question I could ask 51 00:02:37,930 --> 00:02:45,110 is, is the difficulty here equal to, in this case, difficult? 52 00:02:45,110 --> 00:02:49,330 That is, did the user type in "Difficult," capital D? 53 00:02:49,330 --> 00:02:53,380 I'll go ahead and make sure this goes on a new block in my code, 54 00:02:53,380 --> 00:02:54,590 just for style's sake. 55 00:02:54,590 --> 00:02:58,060 And for now, why don't I leave this branch of code as just 56 00:02:58,060 --> 00:03:01,840 dot, dot, dot, meaning I'll get back to it a little bit later. 57 00:03:01,840 --> 00:03:04,190 So this is our first conditional. 58 00:03:04,190 --> 00:03:07,870 We're asking the question, did the player type in, capital D, "Difficult" 59 00:03:07,870 --> 00:03:09,370 for difficulty? 60 00:03:09,370 --> 00:03:12,500 And then whatever code is indented will run 61 00:03:12,500 --> 00:03:18,100 if this condition, this Boolean expression, is true. 62 00:03:18,100 --> 00:03:19,970 So what else could we do? 63 00:03:19,970 --> 00:03:23,840 Well, presumably, the user might not always type in "Difficult." 64 00:03:23,840 --> 00:03:26,000 They could type in "Casual," for instance. 65 00:03:26,000 --> 00:03:29,530 Well, in that case, we could actually specify what should happen in the case 66 00:03:29,530 --> 00:03:32,060 that this condition is not true. 67 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 68 00:03:37,820 --> 00:03:42,940 if this condition is true, difficulty is equal to, capital D, "Difficult." 69 00:03:42,940 --> 00:03:45,460 But otherwise, else, if that's not the case, 70 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. 71 00:03:50,920 --> 00:03:53,980 So a good first question to ask, but now I 72 00:03:53,980 --> 00:03:57,650 think we should ask another question too, about number of players as well. 73 00:03:57,650 --> 00:04:01,900 So if, let's say, the player typed in difficulty "Difficult," 74 00:04:01,900 --> 00:04:03,900 but then we might also want to ask them, well, 75 00:04:03,900 --> 00:04:05,650 how many players do you want to play with? 76 00:04:05,650 --> 00:04:07,750 That could inform our recommendation. 77 00:04:07,750 --> 00:04:10,730 Within this branch, I could also ask another question. 78 00:04:10,730 --> 00:04:17,140 I could say, if players is equal to maybe multiplayer, 79 00:04:17,140 --> 00:04:22,040 dot dot, dot, then we could probably recommend some given game, in this case. 80 00:04:22,040 --> 00:04:25,960 But of course, similar to difficulty, a player or a user could actually type 81 00:04:25,960 --> 00:04:28,012 in something different than "Multiplayer." 82 00:04:28,012 --> 00:04:29,470 They could type in "Single-player." 83 00:04:29,470 --> 00:04:32,170 So we could make use of else here as well-- 84 00:04:32,170 --> 00:04:33,950 else, dot, dot, dot. 85 00:04:33,950 --> 00:04:37,690 So now we're building up the branches of this program. 86 00:04:37,690 --> 00:04:41,470 Within this branch of difficult games, we'll ask the player, 87 00:04:41,470 --> 00:04:43,970 do they prefer multiplayer or single-player. 88 00:04:43,970 --> 00:04:45,860 And if multiplayer, we'll take this path. 89 00:04:45,860 --> 00:04:48,610 If single-player, we'll take this path. 90 00:04:48,610 --> 00:04:52,120 Let's do the same thing down below here, because even if the player specifies 91 00:04:52,120 --> 00:04:56,590 casual, well, we still want to make an informed decision of whether they wanted 92 00:04:56,590 --> 00:04:59,180 multiplayer or single-player games too. 93 00:04:59,180 --> 00:05:04,230 So I'll go ahead and ask the same question-- if players == multiplayer, 94 00:05:04,230 --> 00:05:05,380 just like this. 95 00:05:05,380 --> 00:05:07,420 We'll have one branch of our code. 96 00:05:07,420 --> 00:05:10,360 Else we'll have another branch just like this. 97 00:05:10,360 --> 00:05:14,940 And notice now there are four branches, four places we could end up with, 98 00:05:14,940 --> 00:05:16,500 which makes sense. 99 00:05:16,500 --> 00:05:21,240 If we have given the player two options, difficulty and players, 100 00:05:21,240 --> 00:05:24,180 each of which has two possible inputs, well, there 101 00:05:24,180 --> 00:05:27,430 are four possible games we could recommend. 102 00:05:27,430 --> 00:05:31,410 So if a player wants a difficult multiplayer card game, 103 00:05:31,410 --> 00:05:33,270 maybe recommend to them poker, for instance. 104 00:05:33,270 --> 00:05:36,090 I'll recommend poker. 105 00:05:36,090 --> 00:05:39,570 If they want a difficult single-player game, 106 00:05:39,570 --> 00:05:41,730 we could perhaps recommend to them maybe a game 107 00:05:41,730 --> 00:05:45,790 called klondike, which is the classic solitaire game, if you're familiar. 108 00:05:45,790 --> 00:05:48,330 But down below, let's see down here, if they 109 00:05:48,330 --> 00:05:52,560 want a casual game that is multiplayer, we could recommend maybe a game 110 00:05:52,560 --> 00:05:53,920 like hearts. 111 00:05:53,920 --> 00:05:57,420 And if they want a casual, maybe, single-player game, 112 00:05:57,420 --> 00:05:59,370 we could recommend one-- 113 00:05:59,370 --> 00:06:02,320 maybe one like clock, which is really just luck based. 114 00:06:02,320 --> 00:06:05,170 So here now, we have four games to recommend 115 00:06:05,170 --> 00:06:07,550 based on the input the user gave us. 116 00:06:07,550 --> 00:06:09,830 And this is our conditional structure. 117 00:06:09,830 --> 00:06:11,680 We've used conditionals to actually modify 118 00:06:11,680 --> 00:06:14,920 how our program runs, having it take different pathways depending 119 00:06:14,920 --> 00:06:17,030 on the input the user gave us. 120 00:06:17,030 --> 00:06:18,200 So let's try it. 121 00:06:18,200 --> 00:06:25,780 If I were to, in this case, run python of recommendations.py and enter 122 00:06:25,780 --> 00:06:27,190 "Difficult"-- 123 00:06:27,190 --> 00:06:30,440 let's say "Difficult"-- and "Multiplayer," 124 00:06:30,440 --> 00:06:32,480 which game do you think we should get? 125 00:06:32,480 --> 00:06:34,630 Well, probably in this case poker. 126 00:06:34,630 --> 00:06:36,560 Difficulty was "Difficult." 127 00:06:36,560 --> 00:06:38,830 So we'll be sat inside of this branch here. 128 00:06:38,830 --> 00:06:41,680 Now, players is now going to be "Multiplayer." 129 00:06:41,680 --> 00:06:44,000 So we'll end up at this branch here. 130 00:06:44,000 --> 00:06:45,040 I'll hit Enter. 131 00:06:45,040 --> 00:06:47,380 And we'll see "You might like Poker." 132 00:06:47,380 --> 00:06:48,940 So pretty good. 133 00:06:48,940 --> 00:06:52,250 But what could go wrong in this case? 134 00:06:52,250 --> 00:06:57,770 So maybe the user is a little more malicious than we think they might be. 135 00:06:57,770 --> 00:07:00,830 Maybe they don't want even a casual game. 136 00:07:00,830 --> 00:07:03,950 They want something that's like a medium difficulty as well. 137 00:07:03,950 --> 00:07:05,560 So I'll type in "Medium." 138 00:07:05,560 --> 00:07:10,300 And maybe they want not just a multiplayer or a single-player game. 139 00:07:10,300 --> 00:07:12,980 They actually want a two-player game. 140 00:07:12,980 --> 00:07:17,110 And notice how these inputs are not explicitly talked 141 00:07:17,110 --> 00:07:18,830 about inside of our conditional. 142 00:07:18,830 --> 00:07:20,480 So I'm curious where we'll end up. 143 00:07:20,480 --> 00:07:24,700 If I hit Enter here, we'll see "You might like Clock." 144 00:07:24,700 --> 00:07:27,440 Now, clock is definitively a single-player game. 145 00:07:27,440 --> 00:07:29,870 So we should not have ended up at clock here. 146 00:07:29,870 --> 00:07:32,380 But there is a reason that we did. 147 00:07:32,380 --> 00:07:37,300 So we asked the question here, if difficulty is equal to, capital D, 148 00:07:37,300 --> 00:07:40,130 "Difficult," then we'll do this branch here. 149 00:07:40,130 --> 00:07:44,710 If that's not the case, though, we'll be in this branch down below here. 150 00:07:44,710 --> 00:07:48,670 Now, there are many other options besides just, capital D, "Difficult" 151 00:07:48,670 --> 00:07:49,760 a user could type in. 152 00:07:49,760 --> 00:07:51,740 In this case, I typed in "Medium." 153 00:07:51,740 --> 00:07:54,160 And because that was not equal to "Difficult," well, 154 00:07:54,160 --> 00:07:56,590 we're going to end up in this branch down here. 155 00:07:56,590 --> 00:08:02,060 And similarly, I typed in "Two-player," which is definitely not "Multiplayer." 156 00:08:02,060 --> 00:08:05,030 So we then ended up at this particular branch here. 157 00:08:05,030 --> 00:08:09,140 So else is good when there are no other options you want to test for. 158 00:08:09,140 --> 00:08:12,730 But in this case, I'd argue we want to test if the user actually typed 159 00:08:12,730 --> 00:08:17,103 in, in this case, "Casual" or "Single-player" explicitly. 160 00:08:17,103 --> 00:08:19,520 So let's see how we could do that with these conditionals. 161 00:08:19,520 --> 00:08:23,620 Well, it turns out there's one more keyword beyond if and else, 162 00:08:23,620 --> 00:08:27,610 one called elif, which is kind of an elision, if you will, 163 00:08:27,610 --> 00:08:30,850 a collision of both "else" and "if." 164 00:08:30,850 --> 00:08:37,210 And it's a way of asking some question if the prior condition was not true. 165 00:08:37,210 --> 00:08:41,080 So here, I did ask if difficulty is equal to "Difficult," capital D. 166 00:08:41,080 --> 00:08:46,570 But otherwise, if it's not, elif, we could ask some other question as well. 167 00:08:46,570 --> 00:08:51,910 Maybe I'll ask if difficulty was equal to, in this case, "Casual," 168 00:08:51,910 --> 00:08:56,200 just like this, with a capital C. And that opens up for me some new branch 169 00:08:56,200 --> 00:08:58,840 in my conditional structure. 170 00:08:58,840 --> 00:09:03,940 Now, it seems to me that I actually want to move this conditional structure 171 00:09:03,940 --> 00:09:08,120 inside the branch that explicitly asks for casual games. 172 00:09:08,120 --> 00:09:11,720 So I'll go ahead and copy and paste this and move it inside this branch. 173 00:09:11,720 --> 00:09:16,360 And now what should happen if we end up at this else branch here? 174 00:09:16,360 --> 00:09:18,790 Well, we know the player didn't type in "Difficult." 175 00:09:18,790 --> 00:09:21,040 We know they didn't type in "Casual." 176 00:09:21,040 --> 00:09:23,540 So maybe we could print something else entirely. 177 00:09:23,540 --> 00:09:27,520 We could say maybe "Enter a valid difficulty," 178 00:09:27,520 --> 00:09:31,690 letting them know that we don't really know how to recommend games that are not 179 00:09:31,690 --> 00:09:35,230 either difficult or casual, in this case. 180 00:09:35,230 --> 00:09:37,100 Now, what else can we do inside here? 181 00:09:37,100 --> 00:09:40,690 I think there's still the same problem with these two conditional structures 182 00:09:40,690 --> 00:09:41,300 here. 183 00:09:41,300 --> 00:09:43,310 Let's make use of elif here as well. 184 00:09:43,310 --> 00:09:49,070 I'll say, elif players == "Single-player," just like this. 185 00:09:49,070 --> 00:09:52,930 That opens up a new branch that I can then move "Klondike" into. 186 00:09:52,930 --> 00:09:59,860 And then down below, why don't I print maybe "Enter a valid number of players," 187 00:09:59,860 --> 00:10:01,270 just like this. 188 00:10:01,270 --> 00:10:02,900 I'll do the same thing down below. 189 00:10:02,900 --> 00:10:08,400 In fact, I can copy and paste this, for now, down to this branch here, 190 00:10:08,400 --> 00:10:09,893 our "Casual" branch. 191 00:10:09,893 --> 00:10:12,810 And now, we want to make sure we actually change these recommendations 192 00:10:12,810 --> 00:10:15,970 to "Hearts" and "Clock." 193 00:10:15,970 --> 00:10:19,500 And I think that should be set for us. 194 00:10:19,500 --> 00:10:21,010 So what have we done? 195 00:10:21,010 --> 00:10:23,280 We've made it able for us to actually check 196 00:10:23,280 --> 00:10:26,290 if the user has entered a value different from what we expected. 197 00:10:26,290 --> 00:10:29,290 We're now more flexible and less trusting of the user. 198 00:10:29,290 --> 00:10:30,790 So let's try this. 199 00:10:30,790 --> 00:10:36,030 I'll go ahead and I will run python of recommendations.py. 200 00:10:36,030 --> 00:10:36,900 I'll hit Enter. 201 00:10:36,900 --> 00:10:42,250 And I'll type in, let's say, a "Casual" game, maybe "Multiplayer." 202 00:10:42,250 --> 00:10:45,660 And that should put us at hearts, let's say. 203 00:10:45,660 --> 00:10:46,600 I see hearts. 204 00:10:46,600 --> 00:10:47,278 That's good. 205 00:10:47,278 --> 00:10:48,820 Now let's be a little more malicious. 206 00:10:48,820 --> 00:10:55,440 If I type in something like "Medium" and maybe "Two-player," as well, 207 00:10:55,440 --> 00:10:57,180 where do you think we'll end up? 208 00:10:57,180 --> 00:11:00,880 Well, it seems like we should end up in "Enter a valid difficulty." 209 00:11:00,880 --> 00:11:01,480 So we'll see. 210 00:11:01,480 --> 00:11:02,340 Hit Enter. 211 00:11:02,340 --> 00:11:05,960 And we will see "Enter a valid difficulty." 212 00:11:05,960 --> 00:11:08,960 So I argue that this program should work for us. 213 00:11:08,960 --> 00:11:13,210 If we were to enter in all combinations of "Difficult," "Single-player," 214 00:11:13,210 --> 00:11:16,960 "Multiplayer," or "Casual," we'd get the right game that we would want to be 215 00:11:16,960 --> 00:11:19,090 recommending, in this case. 216 00:11:19,090 --> 00:11:21,920 Now, there are still some improvements to be made. 217 00:11:21,920 --> 00:11:24,770 Notice one here is this idea of copying and pasting. 218 00:11:24,770 --> 00:11:26,410 We can probably do better than that. 219 00:11:26,410 --> 00:11:29,740 But for that, we need to learn more about this idea of a Boolean 220 00:11:29,740 --> 00:11:30,800 expressions. 221 00:11:30,800 --> 00:11:33,330 More on that another time. 222 00:11:33,330 --> 00:11:34,000