Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Determining the number of M&Ms

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, Hello World was fun but it gets old pretty quick and it's not what we're trying to do, is it? We wanted to create this guessing game.
0:07 So, there's a couple of things that we need to do at the beginning; maybe it's worthwhile to do a little printout thing, here.
0:14 Let's go ahead and put a little bit of a header thing like a message that comes out of the top to say Hey people, you're playing this game!
0:21 Now, you're going to see me do a couple of commands when I'm in PyCharm; certain things that'll just happen on the screen
0:27 because there's a keyboard shortcut but there's not really a reasonable menu item to do. So, watch this: I want to have this line
0:34 I want to have one above and one below and then put some stuff in the middle. I can type it again, I can copy and paste it
0:39 but in PyCharm if you just hit Command + D or Ctrl + D on Windows, it will duplicate it but notice down here this duplicate line thing.
0:47 If I do it one more time, it even has the Command I pressed and then the Windows and Linux commands as well. So, this thing I've added to PyCharm
0:54 doesn't come built in with it it's called Presentation Assistant and it'll show you the keys so if you see something happen on the screen
1:00 you'll be like, Oh wait how did that happen or what command was that? Just look down below and you'll see it. This also works for the menu items
1:06 like here we know we can press this but if we press it, you can see run this, would be Ctrl + R. If I wanted to use the hot key or Shift + F10 or
1:13 sometimes F5, depending on how you set up windows. We have this Presentation Assistant set up and that's pretty cool. Let's do one more thing here.
1:20 We want to have some kind of message like M&M Guessing Game, alright? Something like that is going to print out when we run it
1:27 and we'll see it on here, it's our little M&M guessing game. So we're going to take a lot of small step as we do this here.
1:33 Let's do the first thing, as we want to figure out how many M&M's are in the jar. One thing to do would be, something like this.
1:45 I guess we said it's between zero and 100, so lets put 78. Okay there's 78 there. Well, this game's fun once, right?
1:51 But as soon as you've played it once then it's like, Well, I guess that's the end of that. So, we don't know.
1:57 But let's go ahead and have it randomly generate this let's have the computer come up with how many M&M's are in there
2:03 and then we'll play the game based on that number. Now remember, Python has many, many built in libraries
2:09 and features, and one of them is around this random library. And the way we use built in libraries or other libraries
2:14 is we say, import and then we say the library name. And something awesome about PyCharm is it knows all of these things.
2:21 It knows what libraries there are it knows what functions that can be done what operations can be done on various pieces of data and so on.
2:27 If I type r, notice it has random re for regular expression, readline runpy, calendar, all sorts of stuff. But we only have to know if it has something
2:37 to do like random, so type that and then, boom. We get the answer. So that's cool, and then down here we can come down and
2:43 use our random library, so if I type ra, you can see again that's right here, then we hit dot, we have random
2:50 we have choice, but the one we want is randint. Now we're going to come down here this is what's called a function, so we execute it with
2:57 these little parentheses, like so. Just like we were with print. And then you can see over here if I hit Command + P or Ctrl + P on Windows
3:04 it'll tell me the things that have to pass, a and b. Well, that's super not helpful. I guess it takes two things, what are they? I don't know.
3:11 But we can come over here and I say view quick documentation. And it says, this returns a random integer, n such that n is between a and b, inclusive.
3:21 Oh, okay. So, I guess what we want is one to 100, if it's inclusive. Here we go, so let's just print out what we got.
3:28 This is obviously going to ruin the game but just to see that it's working we're going to take this back out soon as we verify everything's okay.
3:34 So I learned from Presentation Assistant that instead of going up here with a mouse I can just hit Ctrl + R, I will. Mmmm, guessing game.
3:42 So 16, 16 is the number of M&M's in the jar at the moment let's run it again. 24. 74. 76. 52. 92. 81. 32 and so on.
3:53 Apparently, yeah, this random is giving us a number between one and 100 as a whole number. So that should work for getting us started.
3:59 Now, we'll be able to use this in the rest of our program. We're going to ask the patron that comes into the store Hey, how many M&M's are in the jar?
4:08 And then we're going to compare it against this. We won't show them that until they win or they give up. Remember, they get five chances.
4:14 And then if they win, or they run out of time we can tell them, Actually there were 76 M&M's in the jar.
4:20 So very cool, very easy to come up with this random number. We don't have to ask somebody Hey, go to the side and enter the number of M&M's.
4:27 Or hard code it so that it's always the same number and the game's only fun once. Where we imported our random and we created a variable
4:35 called M&M_count. This is typcially how you name variables in Python it's all lowercase and then if they're compound words like
4:42 M&M count, then you'll separate that with an underscore. If I'd named it different I'd bet that PyCharm would complain?
4:49 No, not yet, but there are some tools that'll complain that you've misnamed things. Alright, so we've used our random int.
4:55 Which comes with the random library. We come up with this number we're ready to start asking the question How many M&M's are in the jar?


Talk Python's Mastodon Michael Kennedy's Mastodon