Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Getting the guess

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to get the guess from the user
0:02 at least the first guess.
0:04 Before we do though
0:05 I want to show you something really quick
0:06 over here in the GitHub repository
0:07 I've already checked in, you can see in this section here
0:10 I've already checked in the work that we've done
0:13 to do this, so far.
0:14 And with GitHub and source control
0:15 you can see the different versions, over time
0:19 through the history of these various files.
0:22 You can look at what they look like, and so on.
0:23 So I'm going to be saving our code as we go through this
0:26 so you'll be able to jump around in time, basically.
0:30 Alright, back to our game.
0:31 Now, we could do basically two ways here.
0:34 There's a couple of options.
0:36 The pieces we have to work with will only allow us
0:39 to write kind of a crummy version of this
0:41 but let's just get started and then we'll
0:43 I'll teach you one more concept that will
0:45 make this much, much, more nicer.
0:47 So let's come down here and say the the guess
0:50 it's going to be the guess that the user makes.
0:52 So this will actually be the first guess
0:54 and we're going to ask them again.
0:56 Let's do our little print message.
0:59 But do this little print.
1:00 And then if you just do print blank
1:02 that's going to just be a blank line.
1:04 And then over here we're going to show them a message, say
1:06 how many M&Ms are in the jar?
1:09 Remember, the way we do that is with input.
1:11 So we'll say...
1:16 Put a little space so when they type
1:18 it doesn't look like they're typing without proper spacing.
1:21 Now, we would like to compare this against the M&M count.
1:25 But remember, the problem... this is not a number.
1:28 It's not an integer. It's a string.
1:29 And you can't compare, well you could compare them
1:32 but they'll never be the same.
1:33 For example, if you said... is this and this...
1:37 Are they the same?
1:38 They're never the same because
1:39 they're not even the same data type
1:41 as far as Python is concerned.
1:43 It could be confusing at first but that's the way it works.
1:45 These programs are very, very, specific
1:47 and when you compare things
1:49 one of the beginning requirements for them to be equal
1:52 is that they have to be the same type.
1:54 So we'll come over here, and remember the way
1:55 we convert this, is we say guess_text
1:57 and I can even type GT, and notice the two blue letters
2:01 in PyCharm... it's saying allright?
2:03 We're going to just select that for you
2:04 and give me everything that it knows about
2:06 that has a G and a T, with some sort of separation.
2:09 So we're going to do this...
2:10 and then we could print out just the guess real quick
2:13 as a number.
2:14 Let's just see that work so far.
2:16 How many M&Ms are in the jar?
2:18 17? Yes. 17 M&Ms are in the jar.
2:21 But we don't know. Maybe. Maybe not.
2:23 But this is their guess, we've got their guess correctly.
2:27 So what's the problem here?
2:28 I told you this is a little bit simplistic
2:30 and it's not quite going to work.
2:33 Well, the problem is, if they get it right, great.
2:36 They've won.
2:37 But if they get it wrong, remember they have five guesses.
2:41 They have five guesses, and so we have to ask this again.
2:43 So do we then do this again, and this again, and this again?
2:47 Well, that looks horrible
2:48 and, no, we're not going to do that again.
2:51 So we're going to do something different.
2:53 What we're going to do is do something called a loop.
2:56 And there's two type of loop in Python Loops.
2:58 One of them allows us to go through a collection of data.
3:03 Remember we saw those numbers like this?
3:07 If we had some kind of collection of data
3:10 like 1, 1, -2, and 7
3:12 is a really nice way to go.
3:13 I just want to go process them one after another.
3:16 Let me look at one, and then one again
3:17 and then -2, then 57.
3:19 That's very, very, common but in this case
3:21 that's not what we're doing
3:22 and we want to continue to do something
3:24 as long as some case is true.
3:27 The cases, either they've won, or that they they've only
3:31 tried more than the times they can get.
3:33 So I'll say tempt limit, or something like that
3:38 gong to be 5.
3:39 And attempts is going to be 0.
3:41 Alright, so we need to keep track of
3:43 how many times they've tried this.
3:45 And then what we're going to do is we can write this loop
3:47 so we'll say while attempts < attempt_limit.
3:53 Then we're going to let them do it.
3:54 Now when we define things like loops
3:56 or we'll see if statements and conditionals
3:59 what you have to do is
4:00 you have to tell the program, Python
4:03 what part of what... what constitutes the loop?
4:06 What is the thing that you're doing over and over
4:08 and then what happens after the loop is done?
4:11 So notice I typed a colon here.
4:13 That tells me that this is the beginning of the loop.
4:17 If I hit Enter, notice how PyCharm indents like that
4:21 indented four spaces.
4:22 It automatically does that for us.
4:24 And that's because PyCharm knows in order to be in the loop
4:28 you have to indent.
4:29 So we're going to do... actually this one we want up here
4:33 way up higher before we do any of this stuff.
4:35 This is the stuff we want in our loop here.
4:38 So what we're going to do is
4:39 we want to indent that four spaces
4:41 which we can do with just a tab.
4:43 If you want to do more than one line
4:44 you have to highlight them all and hit Tab.
4:45 Then it indents it.
4:47 So the way this little loop thing is going to work
4:49 is it's going to do this, as long as it's true.
4:52 Well, how long is that going to be true?
4:54 Conceptually, you might read it and you think
4:56 well 5 times.
4:57 No, no, no. It's going to do it infinitely many times.
5:01 Why is that?
5:02 Well, it's because this is always 0
5:05 and this is always fine. And 0 is always less than 5.
5:09 What we need to do is, we need to make sure that...
5:11 Let's go ahead and print our guess in here, as well...
5:13 That every time through, we're telling it
5:16 Well, you've made an attempt. So we're going to go from
5:17 0 to 1, 1 to 2, 2 to 3, and so on.
5:20 That's how we do it.
5:21 That's just out here... I'll just print.
5:24 Just to show that this loop is working.
5:26 Let's just run it.
5:27 We're not checking to see if you win or anything like that.
5:30 So let's say 723..4...9.
5:35 We get one more attempt.
5:38 Boom.
5:39 Cool, huh?
5:40 All right so it's much better than writing code
5:42 more than one time.
5:44 Because often you don't know how many times.
5:46 Either we want to do this as many times as they're allowed
5:49 but if they get it right the first time
5:51 we're not going to ask them again. That would be weird.
5:54 So we're just going to exit out of the loop early.
5:56 I'll show you how we can do that, as well.
5:58 Here's how we're going to get input from the user
6:00 as many times as we need.