3.4 Write a number-guessing game - Video Tutorials & Practice Problems
Video duration:
11m
Play a video:
<v ->So now that we've learned all of these concepts,</v> we're ready to work on challenge number two: writing a number guessing game. So I just gave you this problem, you can start writing it right away. But, instead we're going to work on our problem solving skills, and guess and practice. So, this isn't the only form of problem solving but this is the one I wanted to show you today. And the first step is understanding the problem. The problem in this case is not very clear, so we have to flush it out. And then, we want to break it down into smaller sub-problems that are easier for us to work out, one at a time. And then, we will want to convert that into computer logic. So using "if," "else," "not," or an "and," but mostly like "if" and "else." And then, in the future, we'll able to do loops as well. And then we want translate that into code and like, just work on the writing part at the end after we've like... fully understood the problem. And some programmers call it the Eighty-Twenty rule, where you'll think about the problem for eighty percent of the time and actually spend twenty percent of the time writing the code. Another version of the Eighty-Twenty rule is that you'll spend twenty percent of the time writing eighty percent of the functionality and you got it mostly there but then, the whole debugging process and like tweaks to-- get it perfect will take eighty percent of the time to do twenty percent of the functionality. So, here's our example problem. We want to write a number guessing game, some questions to consider are, what the potential numbers could be, how are those numbers determined, or how is that number determined, what happens when they guess wrong, and how many tries do they get. I didn't include what happens if they guess not a number and say guess a letter instead. So the answer to the questions are, let's pick the numbers between one and twenty. That seems large enough that it might be kinda of a more interesting game, not too easy, not too hard, but we can always change that afterwards. And then, if they guess wrong then we want to indicate if the number is higher or lower than the guess, so that they can have like a better chance of getting it next time. And then let's give the user three tries to get it right. After the third try, if they get it wrong, then they lose the game. So the first thing that we'll gonna want to do is choose that number. And let's just use the python random number generator that we've already seen before to choose a number between one and twenty. So this is a first step. And then we want to ask the user to make a guess, and then check if it's correct. So if the number is correct, then tell them they won and quit the game. And then, if the number is higher than the guess, we want to tell them to guess a higher number. And if it was lower, tell then to guess a lower number next time. And then, repeat this three times and then it'll loop, it will go around. We'll do it three times, tell them if they won then you exit, but then, if they didn't then tell them they lost, and exit then. So here's our new problem definition, guess a number between one and twenty and then give them three tries to get it right. Now those are numbers we just picked, we don't know if that creates the best game, but we can tweak them afterwards if the user experience isn't quite right. So remember, for these challenges, feel free to pause the video and try working on it on your own and then run the video if you want to see how I solved it. We also have-- an example solution in here... And, for this one, it would be just challenge to number guess. And you can work off of this, like fleshed out, step by step problem and work on translating it to code yourself. Okay so, we have problem six, this lucky number guess, this is gonna be a lot of the code that we gonna use so now let's open up the challenges and go to challenge number two, this number guess dot pi. And we've got this doc string at the top that tells us exactly that what we wanted to do. And then we want to write some code here, so remember that this is just a stub of a function and we need to actually fill this function out and remove the pass once we're doing something. And then we're calling and running the game here. So the first line that we wanted to do is choose a random number between one and twenty. Let's look at this lucky number guess and I'm just gonna copy and paste this stuff. I can do that, the import should be at the top as a file, you can do it in either location and if it's at the top of the file, then when the file loads then this is important but when it's inside of run_game then when run_game gets called, then it gets imported. And then we want to choose a random number between one and twenty. So we will change this to twenty. And, I can now delete... that stuff. And then next we want to ask the user to make a guess. We want them to guess and we'll use this, so I'll copy and paste that. And it says, "I'm thinking of a number between one and ten." And, maybe we'll have that as... twenty. But do we want to repeat this every time? Probably not. So, I'm going to now... delete that, print just that, and then in the input say, "Make a guess." So that this is the one that we can repeat and this just happens the first time. And it's taking input, converting it to an integer, and saving it as "guess." So next, we can say if the number is correct, tell them they won. So, if... guess is equal to answer, print, "That's it! You got it!" And, we want to exit. So that's part of why I put this inside of a function so now we can just return to get out of that function. And, nothing else will run. Otherwise, if we didn't have this function, we would have to write some codes to close the file or to stop running the file. It's a little harder. Okay, so if the number is not correct, and the number is higher than the guess, then tell them to guess a higher number. So, elif... answer is greater than their guess, print, "Guess higher." So it's very similar to what we had before, but the logic is a little bit different. We could've done it either way. These are the same, they're just like swapped. And then, else, print, "Guess lower." Okay, so that's good. And then now, we get to repeat three times and then tell them that they lost and exit. So, we don't yet know how to repeat things and we will be talking about that in the next chapter. And for now, what we can do is just... copy and paste all this stuff we want to repeat. So, if they get it wrong, ask them to do another guess. And, if this one's right, it will still exit, so that's great. So, first guess, second guess, third guess, and if it gets to here, then we can say, the answer was... This. and we don't have to return anything after that, since there's no more code. But if we did want to, we can return. And, you'll notice that this is underlined in yellow. And that's just like a warning that pi charm has for me to say that, because of pep 8, remember that sis tile guide, it's expecting two blank lines. And also one blank line at the end of the file. Okay, so let's run this. I'll guess ten. Make a higher guess. Fifteen, thirteen. And the number was eleven. So, we could play this a few times, see if the number of guesses was a good amount and maybe like, add if we wanted to have another guess, we can just add that on the bottom, or we'll just add this part. And now it's four times. Okay, great. So this is a lot of code for a pretty simple program, and in the next-- in the next lesson, we'll look at how to refactor this code using loops instead.