1 00:00:00,780 --> 00:00:02,370 - [Narrator] In this video, we're going to 2 00:00:02,370 --> 00:00:05,020 continue our discussion of simulation 3 00:00:05,020 --> 00:00:07,360 with a game of chance, a dice game 4 00:00:07,360 --> 00:00:09,150 called "Craps" that you'll find 5 00:00:09,150 --> 00:00:11,600 played in casinos around the world. 6 00:00:11,600 --> 00:00:14,350 Now the basic rules of the game are as follows. 7 00:00:14,350 --> 00:00:17,520 We're going to start by rolling two six-sided dice 8 00:00:17,520 --> 00:00:20,880 with the values one through six on the different faces. 9 00:00:20,880 --> 00:00:22,620 We're going to calculate the sum 10 00:00:22,620 --> 00:00:25,470 of the two upward faces of the dice, 11 00:00:25,470 --> 00:00:29,320 and if that sum is seven or 11 on the first roll, 12 00:00:29,320 --> 00:00:30,880 the player wins. 13 00:00:30,880 --> 00:00:34,590 If the sum is two, three, or 12 on the first roll, 14 00:00:34,590 --> 00:00:35,950 the player loses. 15 00:00:35,950 --> 00:00:39,560 And if the sum is any of the other possible totals, 16 00:00:39,560 --> 00:00:42,560 that is known as your point. 17 00:00:42,560 --> 00:00:44,860 And in order to win going forward, 18 00:00:44,860 --> 00:00:46,640 you have to keep rolling the dice 19 00:00:46,640 --> 00:00:48,750 and you must make your point, 20 00:00:48,750 --> 00:00:52,070 which is actually where that expression comes from. 21 00:00:52,070 --> 00:00:56,440 So if you roll your point before you roll a seven 22 00:00:56,440 --> 00:00:59,550 on the second and subsequent rolls of the dice, 23 00:00:59,550 --> 00:01:00,510 then you win. 24 00:01:00,510 --> 00:01:03,500 If you roll a seven before rolling your point, 25 00:01:03,500 --> 00:01:04,343 you lose. 26 00:01:05,575 --> 00:01:07,650 So before we get into the actual script, 27 00:01:07,650 --> 00:01:10,910 which uses many of the techniques we've introduced so far, 28 00:01:10,910 --> 00:01:13,780 and introduces a couple of additional ones, 29 00:01:13,780 --> 00:01:17,350 let's switch over to a terminal window here 30 00:01:17,350 --> 00:01:19,940 and execute the script a couple of times 31 00:01:19,940 --> 00:01:22,440 so you can see it in action. 32 00:01:22,440 --> 00:01:26,900 So here you can see, I'm in my lesson four folder, 33 00:01:26,900 --> 00:01:29,770 the scripts that are part of this lesson are listed here, 34 00:01:29,770 --> 00:01:34,300 we're going to be looking at figure 04_02.py 35 00:01:34,300 --> 00:01:35,720 in just a moment. 36 00:01:35,720 --> 00:01:38,883 I'm about to execute that with the iPython command. 37 00:01:40,149 --> 00:01:42,100 Now when I do execute that, in this first case, 38 00:01:42,100 --> 00:01:45,500 it happens to be that we lost on the first roll. 39 00:01:45,500 --> 00:01:48,480 A two three or 12 on the first roll is a loss, 40 00:01:48,480 --> 00:01:50,580 so we display "Player loses." 41 00:01:50,580 --> 00:01:52,990 Let me recall that command and execute it again. 42 00:01:52,990 --> 00:01:54,740 I got the exact same result but with 43 00:01:54,740 --> 00:01:58,880 two different dice faces in this case. 44 00:01:58,880 --> 00:02:00,283 And let's run it again. 45 00:02:01,320 --> 00:02:03,370 So far that's interesting that we've only 46 00:02:03,370 --> 00:02:06,180 had to roll once each time, we won this time. 47 00:02:06,180 --> 00:02:09,019 Let's see if we can get a roll of several. 48 00:02:09,019 --> 00:02:10,360 We lost on one roll that time. 49 00:02:10,360 --> 00:02:11,320 There we go. 50 00:02:11,320 --> 00:02:13,650 All right so in this case, what we had 51 00:02:13,650 --> 00:02:16,360 is three plus six is nine on the first roll, 52 00:02:16,360 --> 00:02:17,560 so our point is nine. 53 00:02:17,560 --> 00:02:21,430 It's not a seven, 11, two, three, or 12. 54 00:02:21,430 --> 00:02:24,146 And then we had a whole bunch of rolls 55 00:02:24,146 --> 00:02:27,670 before we eventually rolled a seven, before rolling a nine. 56 00:02:27,670 --> 00:02:29,660 So we lost in that case. 57 00:02:29,660 --> 00:02:32,360 Let's see if we can get a win after a couple of rolls. 58 00:02:33,268 --> 00:02:36,000 In this case we had two rolls, but we again lost 59 00:02:36,000 --> 00:02:39,210 because we rolled a seven before the point. 60 00:02:39,210 --> 00:02:40,910 So far we're losing a lot, which is why 61 00:02:40,910 --> 00:02:42,060 you don't wanna gamble. 62 00:02:43,160 --> 00:02:45,710 Let's see if we can run this a couple more times. 63 00:02:45,710 --> 00:02:47,360 Oh wow, that's a really long one. 64 00:02:48,348 --> 00:02:51,130 Very rare that you see this many rolls in a row, 65 00:02:51,130 --> 00:02:54,130 and then get your point before rolling a seven, 66 00:02:54,130 --> 00:02:57,430 because seven is the most common sum of the dice 67 00:02:57,430 --> 00:02:59,430 that you'll encounter. 68 00:02:59,430 --> 00:03:02,900 So now that we've seen the script in action, 69 00:03:02,900 --> 00:03:06,350 let's go ahead and take look at the source code 70 00:03:06,350 --> 00:03:08,143 that implements this example. 71 00:03:09,215 --> 00:03:12,330 So as we did in the interactive iPython session, 72 00:03:12,330 --> 00:03:15,410 in order to use the random number generator, 73 00:03:15,410 --> 00:03:18,756 we do have to "import random" first, 74 00:03:18,756 --> 00:03:20,430 so we see that up at the top of the script here 75 00:03:20,430 --> 00:03:23,230 after our comment that says the file name 76 00:03:23,230 --> 00:03:27,350 and our docstring that says the purpose of the script. 77 00:03:27,350 --> 00:03:29,470 Now for this particular program, 78 00:03:29,470 --> 00:03:32,130 we've defined two functions that we're going 79 00:03:32,130 --> 00:03:34,890 to use in the script down below. 80 00:03:34,890 --> 00:03:36,880 And when you're writing a script, 81 00:03:36,880 --> 00:03:38,910 any functions that you're going to call 82 00:03:38,910 --> 00:03:41,710 have to be defined before the statements 83 00:03:41,710 --> 00:03:46,133 that are actually going to call them are executed. 84 00:03:47,376 --> 00:03:49,020 So what we've done here is defined two functions, 85 00:03:49,020 --> 00:03:51,350 one called "roll dice," and you can probably 86 00:03:51,350 --> 00:03:53,770 guess based on its name what it's going to do, 87 00:03:53,770 --> 00:03:56,120 but it's going to roll two dice 88 00:03:56,120 --> 00:03:58,900 by randomly selecting numbers from one through seven, 89 00:03:58,900 --> 00:04:00,800 and then we're going to introduce 90 00:04:00,800 --> 00:04:02,670 a new concept in this example, 91 00:04:02,670 --> 00:04:04,900 which is returning more than one value 92 00:04:04,900 --> 00:04:07,050 at a time from a function. 93 00:04:07,050 --> 00:04:10,180 In particular, we're going to roll two dice 94 00:04:10,180 --> 00:04:14,490 and return both of those die values back to the caller. 95 00:04:14,490 --> 00:04:16,760 We also have a "display dice" function, 96 00:04:16,760 --> 00:04:20,420 which is going to receive what is known as a "tuple," 97 00:04:20,420 --> 00:04:21,700 containing the two dice. 98 00:04:21,700 --> 00:04:24,040 And we'll talk more about tuples in just a moment. 99 00:04:24,040 --> 00:04:27,500 And it is going to pick apart the two die values 100 00:04:27,500 --> 00:04:29,420 and display the contents. 101 00:04:29,420 --> 00:04:31,300 So again, I'm just giving you a kind of 102 00:04:31,300 --> 00:04:34,170 summary of the script first, and then we'll go ahead 103 00:04:34,170 --> 00:04:35,740 and look at the details. 104 00:04:35,740 --> 00:04:40,600 The actual logic of the application starts here at line 16, 105 00:04:40,600 --> 00:04:42,330 where we're going to roll the dice, 106 00:04:42,330 --> 00:04:44,890 display their values, calculate their sum, 107 00:04:44,890 --> 00:04:48,180 and then work through whether we won, lost, 108 00:04:48,180 --> 00:04:50,000 or need to continue rolling. 109 00:04:50,000 --> 00:04:51,680 If we need to continue rolling, 110 00:04:51,680 --> 00:04:53,230 the "while" loop will do that, 111 00:04:53,230 --> 00:04:55,350 and then eventually we'll win or lose 112 00:04:55,350 --> 00:04:57,090 and display the results. 113 00:04:57,090 --> 00:04:59,100 So let's go back to the top and take a look 114 00:04:59,100 --> 00:05:00,690 at this in more detail. 115 00:05:00,690 --> 00:05:02,670 So the "roll dice" function, first of all 116 00:05:02,670 --> 00:05:05,850 I want to point out the naming convention once again. 117 00:05:05,850 --> 00:05:08,580 If you have a multi-word name for a function, 118 00:05:08,580 --> 00:05:11,130 you're supposed to define it with all lower case letters 119 00:05:11,130 --> 00:05:13,483 and separate the words by an underscore. 120 00:05:14,484 --> 00:05:17,434 That's per the style guide, so that's what we've done here. 121 00:05:18,577 --> 00:05:20,640 And this particular function has no arguments, 122 00:05:20,640 --> 00:05:23,360 so we have an empty set of parentheses. 123 00:05:23,360 --> 00:05:25,610 It's simply going to perform a task 124 00:05:25,610 --> 00:05:28,210 and give us back a result. 125 00:05:28,210 --> 00:05:32,180 The colon introduces the block that belongs to the function, 126 00:05:32,180 --> 00:05:34,400 that is the set of statements to execute 127 00:05:34,400 --> 00:05:36,200 when the function gets called. 128 00:05:36,200 --> 00:05:39,750 And as you see here, each of the statements in the body 129 00:05:39,750 --> 00:05:44,170 is indented the same number of spaces. 130 00:05:44,170 --> 00:05:47,360 Per convention, per style, in the style guide, 131 00:05:47,360 --> 00:05:50,690 we start our function's body with a docstring 132 00:05:50,690 --> 00:05:52,610 stating the purpose of the function, 133 00:05:52,610 --> 00:05:56,630 so "Roll two dice and return their face values as a tuple." 134 00:05:56,630 --> 00:06:00,230 So let's talk about that a tuple is for a moment here. 135 00:06:00,230 --> 00:06:02,820 In the preceding lesson, we introduced 136 00:06:02,820 --> 00:06:05,350 the concept of a "list" in Python, 137 00:06:05,350 --> 00:06:07,910 which is simply a collection of values. 138 00:06:07,910 --> 00:06:10,530 As you'll see in lesson five, 139 00:06:10,530 --> 00:06:14,440 a list is actually a modifiable entity, 140 00:06:14,440 --> 00:06:17,070 meaning it can grow and shrink 141 00:06:17,070 --> 00:06:19,010 by adding and removing elements, 142 00:06:19,010 --> 00:06:21,580 and you can change the values of individual elements 143 00:06:21,580 --> 00:06:23,200 within a list as well. 144 00:06:23,200 --> 00:06:25,020 A tuple, on the other hand, 145 00:06:25,020 --> 00:06:28,440 is an immutable sequence of values. 146 00:06:28,440 --> 00:06:31,300 That is, once you create a tuple, 147 00:06:31,300 --> 00:06:34,390 you cannot remove elements from it 148 00:06:34,390 --> 00:06:37,030 or add more elements to it. 149 00:06:37,030 --> 00:06:40,130 So it's a great way to basically 150 00:06:40,130 --> 00:06:42,350 group together some related data, 151 00:06:42,350 --> 00:06:45,823 so that you can pass it around in a Python program. 152 00:06:47,033 --> 00:06:48,540 So with that said, inside the body 153 00:06:48,540 --> 00:06:50,640 of our "roll dice" function here, 154 00:06:50,640 --> 00:06:54,620 we are using the random module's random range function 155 00:06:54,620 --> 00:06:57,090 to produce a random number from one 156 00:06:57,090 --> 00:06:59,040 up to but not including six, 157 00:06:59,040 --> 00:07:01,280 and again those will be integer values. 158 00:07:01,280 --> 00:07:03,500 We call "randrange" twice, which gives us 159 00:07:03,500 --> 00:07:06,740 two separate random values in that range, 160 00:07:06,740 --> 00:07:09,810 and we store those in die one and die two, 161 00:07:09,810 --> 00:07:11,810 and then our "return" statement, 162 00:07:11,810 --> 00:07:15,000 which is going to give back the result of this function, 163 00:07:15,000 --> 00:07:16,663 defines a tuple. 164 00:07:17,932 --> 00:07:19,320 This is called packing the tuple 165 00:07:19,320 --> 00:07:22,050 with the values that you want it to hold. 166 00:07:22,050 --> 00:07:24,410 The parentheses are not required. 167 00:07:24,410 --> 00:07:26,760 What defines a tuple is simply 168 00:07:26,760 --> 00:07:29,730 a comma-separated list of values, 169 00:07:29,730 --> 00:07:31,660 and maybe I shouldn't use the word "list" 170 00:07:31,660 --> 00:07:34,260 in that particular context. 171 00:07:34,260 --> 00:07:38,660 I always tend to use parentheses for readability, 172 00:07:38,660 --> 00:07:42,920 and many programmers do, and there are a couple of cases, 173 00:07:42,920 --> 00:07:47,350 syntactically in Python where those parentheses are required 174 00:07:47,350 --> 00:07:50,543 and if you don't use them you'd wind up with a syntax error. 175 00:07:51,392 --> 00:07:54,710 So I just got in the habit of always including parentheses 176 00:07:54,710 --> 00:07:57,250 for the tuples that I was defining. 177 00:07:57,250 --> 00:07:59,100 So that's how we're going to return 178 00:07:59,100 --> 00:08:01,100 more than one item at a time. 179 00:08:01,100 --> 00:08:03,960 In reality, the one item we are returning 180 00:08:03,960 --> 00:08:07,113 is a tuple that contains two values. 181 00:08:08,310 --> 00:08:12,240 So in reality, every function still returns one value, 182 00:08:12,240 --> 00:08:14,670 but you can pack together a bunch of items 183 00:08:14,670 --> 00:08:18,380 and return them from a function, as shown here. 184 00:08:18,380 --> 00:08:20,210 You can also do that, by the way, 185 00:08:20,210 --> 00:08:23,740 with other data structures like the list data structure 186 00:08:23,740 --> 00:08:27,160 that we talked about in the preceding lesson. 187 00:08:27,160 --> 00:08:30,960 Now our "display dice" function is going to take, 188 00:08:30,960 --> 00:08:33,720 as its argument, what we are calling "dice," 189 00:08:33,720 --> 00:08:37,940 and as you're about to see, "dice" is going to be the tuple 190 00:08:37,940 --> 00:08:40,120 that we return from "roll dice" 191 00:08:40,120 --> 00:08:43,110 so we're going to receive that as one item, 192 00:08:43,110 --> 00:08:45,270 one object if you will. 193 00:08:45,270 --> 00:08:47,890 And then as you can see, this function's job 194 00:08:47,890 --> 00:08:51,210 is to display one roll of the two dice. 195 00:08:51,210 --> 00:08:54,800 This is the key statement in the "display dice" function, 196 00:08:54,800 --> 00:08:57,970 this is called "unpacking" the tuple. 197 00:08:57,970 --> 00:09:00,820 So "dice" is going to be the tuple 198 00:09:00,820 --> 00:09:02,852 containing the two die values, 199 00:09:02,852 --> 00:09:04,810 and when you assign a tuple 200 00:09:04,810 --> 00:09:09,026 to a comma delimited list of variable names, 201 00:09:09,026 --> 00:09:12,070 it unpacks each of the values. 202 00:09:12,070 --> 00:09:14,420 And there is a requirement here 203 00:09:14,420 --> 00:09:17,230 that the number of variables on the left side 204 00:09:17,230 --> 00:09:19,500 of the equal sign must match up 205 00:09:19,500 --> 00:09:22,580 with the number of elements in the tuple. 206 00:09:22,580 --> 00:09:25,740 So because we know that we're packing the tuple 207 00:09:25,740 --> 00:09:28,080 representing the dice with two values, 208 00:09:28,080 --> 00:09:32,610 we know we must use two separate variables 209 00:09:32,610 --> 00:09:34,140 on the left side of the assignment 210 00:09:34,140 --> 00:09:36,230 to unpack the two dice. 211 00:09:36,230 --> 00:09:39,530 And once we've done that, we're using a format string here 212 00:09:39,530 --> 00:09:41,420 to display that the player rolled. 213 00:09:41,420 --> 00:09:43,520 So we insert the value of die one, 214 00:09:43,520 --> 00:09:45,580 and we insert the value of die two, 215 00:09:45,580 --> 00:09:48,810 and then we use that "sum" function 216 00:09:48,810 --> 00:09:51,670 that we introduced in the preceding lesson, 217 00:09:51,670 --> 00:09:53,877 which is built in to Python, to say, 218 00:09:53,877 --> 00:09:57,660 "Here's a sequence that contains a couple of integers," 219 00:09:57,660 --> 00:10:00,647 it's called "dice," it's the argument that we received, 220 00:10:00,647 --> 00:10:03,440 "Walk through that sequence and add up the values." 221 00:10:03,440 --> 00:10:07,400 So rather than us calculating die one plus die two, 222 00:10:07,400 --> 00:10:10,500 we're just using the build in function "sum" 223 00:10:10,500 --> 00:10:12,230 to do that for us. 224 00:10:12,230 --> 00:10:14,290 So now that we've defined these two functions, 225 00:10:14,290 --> 00:10:18,690 we can start to take a look at the logic of the program. 226 00:10:18,690 --> 00:10:21,350 The first thing we need to do when we're playing the game 227 00:10:21,350 --> 00:10:22,800 is to roll the dice. 228 00:10:22,800 --> 00:10:25,370 And as you can see here, we call "roll dice." 229 00:10:25,370 --> 00:10:29,400 Now up above, we just saw that "roll dice" returns a tuple 230 00:10:29,400 --> 00:10:31,430 containing two die values. 231 00:10:31,430 --> 00:10:34,040 And as you can see here, we're storing that 232 00:10:34,040 --> 00:10:36,570 into a variable called "die values." 233 00:10:36,570 --> 00:10:40,510 So if we were doing this in command line 234 00:10:40,510 --> 00:10:42,930 interactive mode with iPython, 235 00:10:42,930 --> 00:10:46,610 we could go ahead and check the type of die values here 236 00:10:46,610 --> 00:10:49,433 and immediately see that it's in fact a tuple. 237 00:10:50,460 --> 00:10:53,230 We could certainly do that here in the script as well 238 00:10:53,230 --> 00:10:55,330 but it's not interactive so we wouldn't 239 00:10:55,330 --> 00:10:59,020 see the results as I type the code here, 240 00:10:59,020 --> 00:11:01,730 that's why I'm just telling you about it at the moment. 241 00:11:01,730 --> 00:11:04,540 So "die values," we then want to display, 242 00:11:04,540 --> 00:11:06,400 so we can see what we rolled 243 00:11:06,400 --> 00:11:08,460 on that initial roll of the dice. 244 00:11:08,460 --> 00:11:12,020 So we take that tuple and pass it off to "die values." 245 00:11:12,020 --> 00:11:14,690 Then we calculate the sum of the dice here 246 00:11:14,690 --> 00:11:18,200 by calling the built in "sum" function once again, 247 00:11:18,200 --> 00:11:21,750 and now we can check to see whether we won the game, 248 00:11:21,750 --> 00:11:24,520 lost the game, or need to continue rolling. 249 00:11:24,520 --> 00:11:27,330 So we have a couple of new features 250 00:11:27,330 --> 00:11:29,880 that we're presenting to you in this example, 251 00:11:29,880 --> 00:11:32,300 one of which was this concept of a tuple. 252 00:11:32,300 --> 00:11:35,520 Another is the "in" operator. 253 00:11:35,520 --> 00:11:38,150 The "in" operator takes the value 254 00:11:38,150 --> 00:11:40,780 on the left side of the operator 255 00:11:40,780 --> 00:11:44,050 and checks whether it's inside the sequence 256 00:11:44,050 --> 00:11:45,860 on the right side of the operator. 257 00:11:45,860 --> 00:11:48,420 And once again we're using a tuple here. 258 00:11:48,420 --> 00:11:50,960 So we have a tuple containing the values 259 00:11:50,960 --> 00:11:54,520 that allow you to win on the first roll, seven and 11. 260 00:11:54,520 --> 00:11:57,710 If the sum of the dice is in this tuple, 261 00:11:57,710 --> 00:11:59,450 then we know we won the game. 262 00:11:59,450 --> 00:12:02,260 So if the sum of the dice is seven or 11, 263 00:12:02,260 --> 00:12:06,320 then we're going to set game status to the string "Won" 264 00:12:06,320 --> 00:12:08,520 to indicate that we won the game. 265 00:12:08,520 --> 00:12:10,670 Otherwise, if the sum of the dice 266 00:12:10,670 --> 00:12:13,450 is in the tuple two, three, or 12, 267 00:12:13,450 --> 00:12:15,590 then we know we lost the game. 268 00:12:15,590 --> 00:12:17,460 So if this condition is true, 269 00:12:17,460 --> 00:12:19,740 we're going to set game status to "Lost." 270 00:12:19,740 --> 00:12:22,500 And for all other sums of the dice, 271 00:12:22,500 --> 00:12:26,230 we're going to set our game status variable to "Continue," 272 00:12:26,230 --> 00:12:29,010 indicating that we need to keep rolling the dice, 273 00:12:29,010 --> 00:12:32,740 and we're going to create a variable called "my point" 274 00:12:32,740 --> 00:12:36,030 in which we're going to store the sum of the dice. 275 00:12:36,030 --> 00:12:37,770 Now for those of you who are coming 276 00:12:37,770 --> 00:12:42,070 from other programming languages into Python, 277 00:12:42,070 --> 00:12:44,500 as you look at this you see we're defining 278 00:12:44,500 --> 00:12:49,110 a variable called "game status" in the different suites 279 00:12:49,110 --> 00:12:51,220 of the "if, elif, else" statement. 280 00:12:51,220 --> 00:12:54,050 We're also defining "my point" in the "else" part 281 00:12:54,050 --> 00:12:59,050 in a C-based language like C, C++, Java, C#, 282 00:13:00,120 --> 00:13:03,280 when you define variables in the block 283 00:13:03,280 --> 00:13:06,470 of an "if, the "elseif," or "elif" 284 00:13:06,470 --> 00:13:08,400 depending on what language you're looking at, 285 00:13:08,400 --> 00:13:10,480 or "else" in those other languages, 286 00:13:10,480 --> 00:13:14,320 that variable would be known only within that block of code. 287 00:13:14,320 --> 00:13:16,850 That is not the case in Python. 288 00:13:16,850 --> 00:13:19,370 When I define these variables locally 289 00:13:19,370 --> 00:13:23,580 within the suites of the "if, elif, else" statement, 290 00:13:23,580 --> 00:13:28,080 they are still known after the statement finishes executing. 291 00:13:28,080 --> 00:13:31,550 So I can use those variables going forward down below. 292 00:13:31,550 --> 00:13:34,830 And that's a very big different between Python 293 00:13:34,830 --> 00:13:37,640 and some of those other programming languages. 294 00:13:37,640 --> 00:13:40,080 So if we get to the "else" part, 295 00:13:40,080 --> 00:13:43,040 we're also going to print out what that sum of the dice was, 296 00:13:43,040 --> 00:13:46,223 so you know what you need to roll to win going forward. 297 00:13:47,420 --> 00:13:51,030 Now the "while" loop is going to keep executing 298 00:13:51,030 --> 00:13:54,100 as long as the value of our "game status" variable 299 00:13:54,100 --> 00:13:57,750 remains equal to "Continue," the string "Continue." 300 00:13:57,750 --> 00:14:00,700 And at that point we need to roll the dice again, 301 00:14:00,700 --> 00:14:03,200 so one of the reasons we defined "roll dice" 302 00:14:03,200 --> 00:14:05,480 was so that we didn't have to duplicate that code 303 00:14:05,480 --> 00:14:08,873 in multiple locations throughout the program's logic. 304 00:14:09,734 --> 00:14:12,200 So we roll the dice and display the new sum of the dice, 305 00:14:12,200 --> 00:14:15,600 and what the actual individual die values were. 306 00:14:15,600 --> 00:14:18,010 Then we calculate the sum of the dice 307 00:14:18,010 --> 00:14:21,220 so that we can check whether we won the game 308 00:14:21,220 --> 00:14:24,300 or lost the game on this subsequent roll. 309 00:14:24,300 --> 00:14:27,390 If the sum of the dice is equal to "my point," 310 00:14:27,390 --> 00:14:29,760 that would mean we won, in which case 311 00:14:29,760 --> 00:14:31,880 we set "game status" accordingly. 312 00:14:31,880 --> 00:14:34,540 Otherwise if the sum of the dice is equal to seven, 313 00:14:34,540 --> 00:14:35,840 that would mean we lost. 314 00:14:35,840 --> 00:14:38,080 So we set "game status" accordingly. 315 00:14:38,080 --> 00:14:41,210 And if we did not modify "game status" 316 00:14:41,210 --> 00:14:43,730 as a result of this "if, elif" statement, 317 00:14:43,730 --> 00:14:45,300 meaning it was not the point 318 00:14:45,300 --> 00:14:47,420 and it was not a seven that we just rolled, 319 00:14:47,420 --> 00:14:49,510 we wind up back at the top of the loop, 320 00:14:49,510 --> 00:14:51,890 to do the next roll of the dice. 321 00:14:51,890 --> 00:14:55,350 And then finally, once we do win or lose, 322 00:14:55,350 --> 00:14:57,080 we check at the end of the game here 323 00:14:57,080 --> 00:14:59,170 to see if "game status" is equal to "Won," 324 00:14:59,170 --> 00:15:01,530 in which case we will display "Player wins" 325 00:15:01,530 --> 00:15:04,407 otherwise we display "Player loses." 326 00:15:05,541 --> 00:15:06,980 So just summarizing a little bit 327 00:15:06,980 --> 00:15:09,480 of what we looked at in this example, 328 00:15:09,480 --> 00:15:13,450 this was our first example with multiple functions defined, 329 00:15:13,450 --> 00:15:16,630 but the key new features that we were introducing 330 00:15:16,630 --> 00:15:20,960 in this example were the ability to return multiple values 331 00:15:20,960 --> 00:15:24,850 from a function by packing them into a tuple, 332 00:15:24,850 --> 00:15:29,210 the ability to unpack a tuple into individual variables 333 00:15:29,210 --> 00:15:32,300 so that you could use the values within the tuple, 334 00:15:32,300 --> 00:15:36,840 and this new "in" operator which we had not seen previously, 335 00:15:36,840 --> 00:15:40,930 which allows us to check whether a value is in a sequence. 336 00:15:40,930 --> 00:15:43,640 And that will work, by the way, with tuples. 337 00:15:43,640 --> 00:15:47,110 It will also work with lists and other sequences 338 00:15:47,110 --> 00:15:49,510 as we work our way forward in Python 339 00:15:49,510 --> 00:15:51,823 and learn other data structures.