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