In this tutorial we will make the number guessing game in Python, also known as the high-low game.

That is, let’s imagine that the computer thinks a number in a certain interval and we have to try to guess it.

number guessing game in Python – first solution

Guess a number between 1 and 100, but with a maximum of 10 attempts.

To develop the solution we will use some functions.

Randrange() function

First we need to generate a random number.

To generate a random number in Python we can use the randrange() method, which generates a random number in a specified range.

Here is the syntax:

random.randrange(start, end + 1, step) #step is optional

That is for example if we want a number between 2 and 7 we have to set randrange(2,8).

But be careful, to use this method we have to import the library that contains it.

We then use this command:

import random

Therefore, after generating the random number, we can continue to implement our game.

We set the maximum number of attempts to 10 using, as already explained, the range() function.

Then we enter a number between 1 and 100 as input and check with the if statement if this number is the same as the one thought up by the computer. If it is the same then we output the ‘guessed’ message and exit the program with the break function.

Otherwise we check if we still have attempts available. If we have any, we also check if the number we entered is less than the one thought by the computer. If so, we display the ‘higher’ message in the output to suggest entering a higher number.

Conversely, if the number is greater than the number thought up by the computer, we give the ‘lower’ message to suggest entering a lower number.

Finally, if we have run out of attempts, we notify the user with an output message.

Here is the complete code of number guessing game in Python:


import random #import the library
n = random.randrange(1,101) #generate a random number between 1 and 100
for i in range(10):
    x = int(input('Enter a number between 1 and 100:'))
    if x == n: #if you guessed right
        print('You guessed the number attempt:', i + 1)
        break #exit the program
    elif i < 9: # else if we're still in range
        if x < n: #evaluate if the number is lower than what the computer thought
            print ('Enter a higher number')
        else: #evaluate if the number is higher than the computer thought
            print ('Enter a lower number')
    else:
        print ('I\'m sorry, you have run out of attempts')

N.B. We check if i <9 as i becomes equal to 9 only after I enter the last number.

We can try the code in Python compiler online, in that link: Compiler Python online.

number guessing game in Python - second solution

Here is another possible solution to the game.


import random #importiamo la libreria
n=random.randrange(1,101)  #generate a random number between 1 and 100
for i in range(10):
    x = int(input('Enter a number:'))
    if x == n: #if you guessed right
        print('You guessed the number attempt:', i + 1)
        break #we leave the loop
    elif x < n: #if the number is lower than what the computer thinks
        print('Higher')
    else:
        print('Lower')
if x! = n:
    print('I'm sorry, you have run out of attempts, the number was', n)

number guessing game in Python - third solution

In the latter example we do not establish a predefined number of attempts but we exit the cycle only after having guessed the number. we will therefore use an indefinite cycle.


import random
n = random.randrange (1,101)
guessed = 0
while guessed == 0:
    x = int(input('Enter a number:'))
    if x == n:
        print('You guessed it')
        guessed = 1
    elif x < n:
        print('Higher')
    else:
        print('Lower')

As you can see there can be various ways to implement number guessing game in Python. Feel free to propose your solution in the comments below.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot