Now let’s write a script which implements a basic “Guess the number” game. The rules of the game are:

- The user selects if he/she wants to guess a number between 1 and 100 or 1 and 1000
- Based on the number-range the user has a fixed number of guesses
- The application generates a number to guess
- The user enters a number
- if it is the secret number the application congratulates the user and ask if he/she wants to play another round or not
- else the application tells the user if the secret number is less or greater than the number provided
- the guess count is increased

- if the user used up all guesses then the application tells him/her the secret number and asks if the user wants to play another round or not

As you can see, the rules are simple but seem complicated to implement. It is up to you how you want to handle wrong input types (no numbers).

Well, it is not as difficult as it sounds. Let’s see some example output of my solution:

Should the secret number between 1 and 100 or 1 and 1000? 100 You have chosen 100, you will have 7 guesses to find the secret number. I have chosen the secret number... What's your guess? 34 The secret number is higher... What's your guess? 54 The secret number is higher... What's your guess? 66 Congrats, you have Won! The secret number was 66 Do you want to play another round? (yes / no) yes Should the secret number between 1 and 100 or 1 and 1000? 1000 You have chosen 1000, you will have 10 guesses to find the secret number. I have chosen the secret number... What's your guess? 500 The secret number is lower... What's your guess? 400 The secret number is lower... What's your guess? 300 The secret number is higher... What's your guess? 350 The secret number is higher... What's your guess? 375 The secret number is lower... What's your guess? 370 The secret number is lower... What's your guess? 360 The secret number is higher... What's your guess? 365 The secret number is lower... What's your guess? 364 The secret number is lower... What's your guess? 363 The secret number is lower... Sorry, you lose. The secret number was 362 Do you want to play another round? (yes / no) no

As you can see the application does not stop when the guesses run out and it displays a message if the user won or lost.

Let’s look at the code.

__author__ = 'GHajba' import random while True: while True: try: max_number = int(input('Should the secret number between 1 and 100 or 1 and 1000? ')) except ValueError: print("This was not a number!") continue if max_number != 100 and max_number != 1000: continue else: break if max_number == 100: guess_count = 7 else: guess_count = 10 print('You have chosen {}, you will have {} guesses to find the secret number.'.format(max_number, guess_count)) secret_number = random.randint(1, max_number) print('I have chosen the secret number...') guesses = 0 while guess_count - guesses: try: guesses += 1 guessed = int(input("What's your guess? ")) except ValueError: continue if guessed == secret_number: print('Congrats, you have Won!') break elif guessed > secret_number: print('The secret number is lower...') else: print('The secret number is higher...') else: print("Sorry, you lose.") print("The secret number was ", secret_number) answer = '' while answer.lower() not in ['yes', 'no', 'y', 'n']: answer = input("Do you want to play another round? (yes / no) ") if 'no' == answer or 'n' == answer: break

As you can see, the code is quite heavy because we include a lot of loops to verify the input and to handle the main game loop. And perhaps you will find this code later not readable at all. To solve this problem we will learn about functions and then we will refactor this piece of code to use functions.

However there are some alternative solutions to this script too. For example you can change the while loop in the middle to a for loop:

for guesses in range(guess_count):

In this case the loop iterates through the range of guesses and ends if the last number is reached.

## Leave A Comment