Video version:
Let’s have fun coding a “Guess the number” game.
Here, we will code a game where you have to guess a number between 0 and 100. Beware, you only have 6 tries! This is the result:
As you can see, the game is easy: Keep asking the player for a number between 0 and 100 (you can change this) until the player guesses the correct number…or runs out of tries.
If you have viewed my previous lessons (if not, now is a good time!) you can do this on your own. Just keep asking the player for a number, compare it with the number to guess until the player runs out of tries or guesses the number! Display some information to the player about how close is to the number and that’s it!
Now, stop reading after this paragraph and try to build the game on your own. It doesn’t matter if it takes you 5 minutes or 2 hours. You will learn more trying to solve it and failing to it than copy-pasting code.
My solution
Have you been successful building it on your own?
If not, this is my code:
guess_number = 78 while True: player_number = int(input("Guess the number:\n")) if player_number == guess_number: print('Congratulations, you guessed the number') break elif player_number > guess_number: print('Too high') elif player_number < guess_number: print('Too low!')
This is a simple version. We type the number and we loop over the code over and over until we guess the number (hence, the ‘break’ line)
But..that’s boring. We already know that the number to guess is 78! The problem is that we can’t create a random number in Python…or can we?
Even if you didn’t know about it, a quick search on Google points us that there is a package in core Python that let us generate random numbers. Cool, that’s what we need it! So let’s import it and generate our own random numbers!
from random import randint # new line guess_number = randint(0, 100) # new line while True: player_number = int(input("Guess the number:\n")) if player_number == guess_number: print('Congratulations, you guessed the number') break elif player_number > guess_number: print('Too high') elif player_number < guess_number: print('Too low!')
Nice! Try the code and every time the number would be a random generated number between 0 and 100 (both included)!
But… it is still too easy! The player can keep guessing without end until the number has been guessed. At the end, everybody wins and there is no chance to fail. Let’s end that
Now, we are limiting the player to only 6 tries. After 6 guesses, if the player hasn’t guessed the number, losses:
from random import randint guess_number = randint(0, 100) tries = 6 # new line while True: player_number = int(input(f'You have {tries} tries.\nGuess the number between 0 and 100:\n')) if player_number == guess_number: print(f'Congratulations, you guessed the numbers and still had {tries} tries left!') break elif player_number > guess_number: print('Too high') elif player_number < guess_number: print('Too low!') tries -= 1 # new line if tries <= 0: # new line print("Sorry, but you didn't manage to guess the number") # new line break # new line
Nice. Now the player has to guess the number in 6 tries or less. Now it is exciting, sometimes the player will win, sometimes will lose.
Improving the code
As you can see, with less than 20 lines we have created a functional mini-game. But there is room from improving. But it is your job now!
Use your imagination to add more features to the game. Perhaps you want to add a second player and compare how many tries they needed to guess the number and the player with fewer tries, win. Or maybe store the score and create a Top ten scoreboard. Or maybe…
Just add your flavour to the game. Try to add a feature, no matter how small it seems. You will enjoy it and you’ll learn more because no one is guiding you and you are coding what you want. That’s the second lesson of today.
And please, leave me a comment with your code. I want to see what you have built and what features you have added!
The REPL with my code: https://repl.it/@DavidMM1707/07-Guess-the-number
Find me on Twitter at: https://twitter.com/DavidMM1707
My GitHub: https://github.com/david1707
Video version: https://www.youtube.com/watch?v=gH_lPWLbYjU
Eighth lesson: 08 – Functions