# Python for Absolute Beginners Transcripts Chapter: Code that interacts with users Lecture: Checking for a win

0:00 We're getting the input, the guesses from the user but, we're not comparing them against the actual M&M count. So, instead of just printing out
0:08 which is just silly let's do that. So what we're going to do, is we're going to do another type of decision making in code.
0:15 This actually is a way of doing as well either you go in the loop or you go in the loop a bunch of times or you might not even go in this loop.
0:21 But the most fundamental piece that we're going to work with is this idea of an if statement. So we'll say if and then we put some kind of test.
0:28 Very much like this, we'll come down here. And we're going to say is the M&M count equal to a guess.
0:34 Now, in programming, this equal means take the value of guess and assign it to be the value of M&M count. So, in these tests we have equal equal.
0:44 And then, just like all the other blocks of code that we talked about, like with... Wow, we're going to put a colon.
0:49 And then we're going to do something cool, here. We're going to say you win or something like that. Let's say print out, you got free lunch.
0:59 It was, and let's put out the number, here. And remember, that's an f-string. We could use guess, we could use M&M count
1:06 doesn't matter, they're the same. We could come down here and say else, but remember we're going to give them some kind of guidance
1:13 if it's too low or too high. So, we don't want to just say, nope! Try again. So we're going to say else if, and then Python. That's shortened to.
1:23 And we want to say, M&M count. Oops, M&M count. Is less than guess, so it it's too low we'll print... Sorry, that's too low.
1:33 And then again, if it's not equal and it's not too low, then, it's going to be too high so we can just say, finally fall through this other case.
1:41 That's too high, like this. And let's make low and high super obvious. And we'll just do it like this for now so they can just
1:49 quickly see, yep, low high. All right, well, that's great but when they win we don't want to ask them again.
1:55 Right now, we're going to go through. Let's go ahead and just run this and see what happens. How many M&M's are in the jar?
2:00 I'm going to use my binary search algorithm we talked about, 50 and that's too low, okay, 75. Too low. 85, too low. 90. 95, woo! Ah, too low.
2:12 Try again. It's not that many guesses, is it? 50! 75. 85. 100. looks like we have some kind of problem here. What is going on, if it's the same?
2:27 Ooh, whoops, I tricked myself there. I should probably think of it like this way. Guess is less then M&M count. So we were saying when it was too high
2:35 actually that's too low. And that was obviously drive me to do it the other way. So let's do it like this. So, if the guess is less then it's too low
2:42 otherwise it's too high. Now, let's do our binary search. 50, that's too low. 75, too high. Ooh, that looks like it's working. 65, 60.
2:53 Alright, we know it's between 50 and 60. Alright, yeah, so let's see, it's going to be 54. Not good odds, but yes! Nailed it, look at that. I got it.
3:02 I had 1 in 10 odds and I still guessed it. So, you saw how the binary search algorithm narrowed it down but remember, this is a contest.
3:09 You win a free lunch, it's not just going to let you ask forever. So yeah, we got pretty lucky there and got our free lunch. So, that's cool.
3:16 Now, it's a little bit, uh... Let's just do 1, a little hack here Comment this out with a hash and just put that as 7.
3:26 Cause I want to know what this is cause I want to show you what's wrong, watch if I guess 10, that's too high, and if I say 5 too low, and I say 7
3:35 yes, free lunch was 7! Now it's asking, hmm... How many are there? 7. Again, it's still 7. All right, I got 3 free lunches or something.
3:45 So, we got a little more work to do. But, we did get our test here to actually check did you win? is it low? Is it too high? That's working.

Talk Python's Mastodon Michael Kennedy's Mastodon