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

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