4.2 Improve the number guessing game with "while" loops
4: Lists and Loops
4.2 Improve the number guessing game with "while" loops - Video Tutorials & Practice Problems
Video duration:
8m
Play a video:
Video transcript
<v ->Okay, so now that we know how to use while loops,</v> let's see if we can incorporate it into our number guessing game. So, a common saying in programming is keep it dry and dry stands for don't repeat yourself so if you've got some repeated code, it means that you can probably change the code and reduce the number of lines. So let's go back to our number guessing game and this is what it looks like right now and we have quite a bit of repeated code in here. If you remember, we just copied and pasted so that's a good sign that we can make some changes. So there is a few ways we can do this. I want to repeat this block of code. So let's put a while loop in here and we have to figure out what condition we want so it could be that we have num guesses is equal to three and we can say while num guesses is greater than or equal to zero. Or is it one? Three, two, one, zero, no maybe just like greater than zero. And then, let's see if I run this I'm gonna get an error because there's an indentation error, it expected an indented block. That's because we need stuff to go inside the while loop so I'm going to select all of this and tab so it goes in all four spaces. And then we can delete all of this stuff. And at the very end let's keep this. So let's just run this and see what happens. So I'm gonna make a guess, ten. Great, I got it. (laughter) I'll make a guess again. 14, guess lower. So seven, guess higher, let's guess 15. We know that's wrong. And then guess again, 20. It is not changing, I just keep being able to guess things. So, this is a problem and it's because num guesses is never getting down to zero. One thing that we could do too is tell the user how many guesses they have left. Let's do that. So print you have left. Let's say you have this number of guesses left. Three guesses left, make a guess. Guess lower and it says three again because I haven't decreased it each time so let's just put it at the end here. So num guesses minus equals one. Okay, let's run again. Guess lower, that's good it's going down. I'm gonna guess again, I have one guess left. Okay and so it's doing it three times, giving me three guesses and then tells me the number was four. Now the only problem here is that this guess lower prints out but I don't want to actually guess again because well, I don't want this to print out because I have no more guesses so I shouldn't say to guess lower. So this looks like it would be good but there's actually this weird little snag that we now have to fix. So there's a number of ways of doing that. I can check to see if num guesses equals zero before doing this part. So I could have another elif statement here. Num guesses equals zero and then break to go out to here. Let's see. So that's good, oh no it's not good. And that's because num guesses is being decreased here so it's not zero yet until it gets to the bottom so I can put it over here and so after a guess is made, now they have no more guesses left and then I can check these things. So you can see that it does make the program a little bit more complicated. You have to think through logically it's gonna do this, then this, then this. I'll do 20 again, make a guess. Great and the number was 17 and it didn't ask me to guess lower. So that's good and now the only thing here is now that we're actually breaking when num guesses equals zero, we actually don't even need this anymore. So let's try again. And it still breaks after three guesses and tells me what the number was. And yeah, so that's good. So if we want to now increase or decrease the number of guesses, we can do that here. Maybe I'll get five guesses and I'll try to get it right, higher. And I got it after one, two, three, four tries. So that condition works so maybe four is a good number or maybe three was a good number cause that was a little too easy. And then also, we have added in this line to say I have this many guesses left. If I had not used this while loop and I wanted to actually insert this into the previous iteration of this code then I would have had to put this in in three different places. So by not repeating yourself, you save yourself future work if you wanna change some of that code up and I only had to add one line instead of having to add it the number of times I wanna call it. So in this case, three. Okay, so this is in here, challenge 2b, number guess with while and I'm using looping to reduce the amount of code I have to write.