Yesterday, we explained in depth some of the functions that are included in the random module in python. Today, we will be applying that knowledge to build a simple guessing game. This is how the game goes.
The computer will think of a random number between 1 and 20 inclusive and ask the player to guess it. The player has 6 tries to guess it right. For each guess, the computer will tell the player if the guess is too high, too low, or the correct guess. After 6 tries, if the player has not correctly guessed the number, the game will end.
Interesting, not so? Well, here is the code for the game.
Now, let’s explain the code line by line so you understand what is going on.
Line 1: To use the random number functions, we have to import the random module. So, that is what this does.
Line 3: using the random.randint function with arguments 1, 20, we are asking the computer to generate a random number between 1 and 20. Simple. I believe you understood this line.
Lines 4-7: The computer asks the player to input his or her name, initializes num_guesses, the counter we use to calculate how many guesses the player has made so far, and sets the guessed_right switch to False to indicate that the player hasn’t made the right guess yet.
Line 9: Using a while loop, we wait for the player to make at most 6 guesses until he guesses the number right or fails to guess it right. Now, here comes the interesting parts after this line.
Lines 10-28: This is the main logic of the game and it occurs inside the while loop. First the computer informs the player of how many guesses have been made so far and asks him or her to make a guess. We use a try-except-else clause to catch the guess entered. If the player enters a guess that is not a number, such as a float or string, that is an error and the program catches it as an error and penalizes the player for the error by increasing the number of guesses made by 1. But if the player enters a number, it skips through the try-except block and goes to the else block which starts at line 16. From here on we are checking to see what number the player entered. The first conditional checks for whether the number is within the acceptable range. If it is not, the program asks the player to enter a number within the acceptable range and penalizes him by increasing the guess count by 1. The next elif blocks checks if the number, which is within the range for acceptable numbers, is too low or too high. If either of these, the number of guesses count is increased by 1. But if these two are skipped that means the player guessed the right number and the guessed_right switch is set to True. The program then breaks out of the while loop.
Elegant! Not to mention beautiful.
Lines 30-33: This is the cleaning up code. Here after the loop exits the program checks whether the player guessed right or not. If he or she guessed right, the program prints a congratulatory message and tells him how many guesses he took. But if the player guessed wrongly, the program encourages the player to try again and then tells the player what the number was.
You could write your own code that implements the functions in the random module. It is a really useful module. I wish you success in doing so.
You can download the script for the game here if you want to run it on your own machine.
Well written codes - good explanation
ReplyDeleteThanks for the encouragement. Did you play the game? If you did, did you win?
Delete