Guessing games are fun. Why don't we try one? Ready?
Let's match on. The python code below illustrates how to play the game
Guess The Number Game
We first of all import the modules we are going to need for the game. We will be using the random module
The below code takes an input from the user and stores it in a variable, name. This is intended to save the name of the user.
name = input('Please enter your name: ')
The ‘introduction’ variable contains a welcome message to the Guess The Number Game.
introduction = 'Hello ' + name + ', you are welcome to the guessing game'
This line prints the ‘introduction’ variable
Guess count and correct guesses of 0 are initialized and the guess limit set to 5.
guess_count = 0 correct_guess = 0 guess_limit = 5
This block of code utilizes a while loop. The condition for the code to run is when the guess count is less than the guess limit. Within the loop, an a numeral input between 0 and 5 is taken from the user and converted to 'integer'. The input is stored as 'guess'. The computer generates a random number between the same range and stores it as 'hidden_number'. A comparison between 'guess' and 'hidden_number' is done. If both are the same, a sentence affirming a correct guess with the 'hidden_number' is printed and the number of correct guesses is increased by 1. If both numbers are different, a sentence affirming a wrong guess with the 'hidden_number' is printed. The guess number is updated to reflect the number of games played. The loop stopes when the condition is invalidated, ie 'guess_count' is greater than 'guess_limit'
while guess_count < guess_limit: guess = int(input('Please guess a number between 0 and 5: ')) hidden_number = random.randint(0,5) if guess == hidden_number: print('You guessed correctly. The hidden number is ' + str(hidden_number)) correct_guess += 1 else: print('You guessed wrongly. The hidden number is ' + str(hidden_number) + '. Try again') guess_count += 1
This block of code simply displays the number of correct guesses at the end of the game
if correct_guess == 1: print('You have come to the end of the game. You had ' + str(correct_guess) + ' correct guess') else: print('You have come to the end of the game. You had ' + str(correct_guess) + ' correct guesses')
Here is a link to the repo on github