Today is my day n I am in the US. I feel an urge to do something to give back, as much and as fast as I can. There is no big idea at the moment, I only know that I should start by writing down. I thought of ginger so I will call this series as Ginger, I will write about how I have helped kids to get exposed to programming. The first is Guessing Game.
This is how the game works, please ignore the code and try this out. We will use Trinket as a platform to jot down the code and Python as our language to implement.
First thing first, what do we need to make the game happen?
- A secret number
- Guess input from user
- Feedback: “too small” if guess is smaller than secret number, “too big” if guess is greater than secret number, “correct” if guess is equal to secret number
Alright, how do we get a secret number? It would have to be randomized within a range, so that the same user can always replay the game. So we need to import random module, then initiate a variable called secret_number (or however you want) and use randint() with the range to get the secret number.
The first step is done. We move on the second, collect guess input from user. Python has the built in function called input() to get input directly from user typing. Great, let’s get it! To call this input function, we also need to pass in question text as parameter, this will be displayed to prompt users. And do not forget to initiate a variable to store the input, in this case, I call it ‘guess’.
Yay! Step 2 is done. Let’s give some feedback. Seems like we need to compare the guess with the secret number. And to tie the comparison with the feedback, we need some sort of conditional statement here.
Perfect, but watch out, we might have an error look like this:
Guess what, any input from input() built in function is always in string, no matter what number you put in while we cannot compare a string with a number. Therefore, we need to convert the guess into an integer. We can use int() built-in function to wrap around guess.
Pheww! One bug was removed. So are we done? Well, what if a user inputs not number but a random text or an invalid number. We need to verify the input before doing the comparison, otherwise, it can screw up the entire program. Try and except is a great way to implement this, we just need to try to convert the guess into an integer, if it does not work out, it will go to except instead of throwing an error or no response for the user.
Nice, the thing is after inputing a guess, the user is stopped, he does not get to the correct one. We need to fix this. We need something to help it run over and over, that is where a while loop comes in.
We can always start with ‘while True’, but please add in a condition later to stop it, otherwise you might run into infinite loop.
Oh lah, we are almost there. One last thing, if you run you will notice that even if you get it correct, it still keeps asking your guess, which is quite problematic.
Do not worry, we can prevent this by having a variable to keep update whether the user gets correct answer yet.
You made it, this should work unless you want to expand your creativity :)
Comment here if you run into any error or have any thoughts. Thanks for reading and Happy coding!