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