Create a simple guessing game in python

Creating a simple guessing game is a great way to get familiar with the basics of python while also creating something that you can enjoy playing. Let’s code a simple guessing game in python. The script will work in that the program will randomly select a number within the range of 1…100. Then, the program will prompt you to guess the number within the range. If your guess is larger than or smaller than the selected number, the program will let you know. The program terminates once the correct number is discovered. Let’s break down the logic step-by-step:

a) Need the program to auto generate a number in the range of 1…100.
b) Need the program to read in user input to allow user to make a guess.
c) Need the program to loop indefinitely until the user enters correct input.
d) Need the program to provide user feedback depending on their guess. I.e, was their guess
greater than, less than, or equal to the secret number?
e) Need the program to print the number of tries it took to reach the secret number.

Below is the python implementation of each corresponding part of the logic:

a) This can be accomplished by using the randint() function in the random module as shown below:

random_num = random.randint(1, 100)

b) This can be accomplished by passing in the input() function to the int() function. The input() function by default reads in strings which is not what we want. Therefore, to ensure that the input we read are numbers use the int() function.

your_guess = int(input())

c) This can be accomplished by using a while loop:

while True:
    body statements

To exit, once the user input matches the secret number we can use a break statement.

d) This can be accomplished by using conditional statements within the while loop as indicated below:

random_num = random.randint(1, 100)
while True:
    your_guess = int(input())
    if your_guess > random_num:
        statements
    elif your_guess < random_num:
        statements
    else:
        statements

e) This can be accomplished by using a variable to keep count during each iteration:

tries, random_num = 0, random.randint(1, 100)
while True:
    your_guess = int(input())
    if your_guess > random_num:
        statements
        tries += 1
    elif your_guess < random_num:
        statements
        tries += 1
    else:
        statements
        tries += 1

Below is the complete code:

>>> import random
>>> # random_num is the number in range 1 ... 100
... # tries keep count of how many guesses player makes
... tries, random_num = 0, random.randint(1, 100)
>>> while True:
...     print('Guess a number in the range of 1-100')
...     your_guess = int(input())
...     if your_guess > random_num:
...         print('{} is greater than the random number X'.
...               format(your_guess))
...         tries += 1
...     elif your_guess < random_num:
...         print('{} is less than the random number X'.
...               format(your_guess))
...         tries += 1
...     else:
...         print('Congrats! {} is equal to the random number '
...               '{}.'.format(your_guess, random_num))
...         tries += 1
...         print('It took you {} tries'.format(tries))
...         break
... 
Guess a number in the range of 1-100
10
10 is less than the random number X
Guess a number in the range of 1-100
20
20 is less than the random number X
Guess a number in the range of 1-100
30
30 is less than the random number X
Guess a number in the range of 1-100
40
40 is less than the random number X
Guess a number in the range of 1-100
50
50 is less than the random number X
Guess a number in the range of 1-100
60
60 is less than the random number X
Guess a number in the range of 1-100
70
70 is greater than the random number X
Guess a number in the range of 1-100
80
80 is greater than the random number X
Guess a number in the range of 1-100
75
75 is greater than the random number X
Guess a number in the range of 1-100
73
73 is greater than the random number X
Guess a number in the range of 1-100
72
72 is greater than the random number X
Guess a number in the range of 1-100
71
71 is greater than the random number X
Guess a number in the range of 1-100
70
70 is greater than the random number X
Guess a number in the range of 1-100
65
65 is greater than the random number X
Guess a number in the range of 1-100
62
62 is greater than the random number X
Guess a number in the range of 1-100
60
60 is less than the random number X
Guess a number in the range of 1-100
61
Congrats! 61 is equal to the random number 61.
It took you 17 tries
============================================================================ Want to learn how to use Python's most popular IDE Pycharm? In the free pdf guide "Getting the Hang of PyCharm" you'll learn all of the amazing features in PyCharm along with how to get started with data science. Subscribe to the Purcell Consult newsletter and get started A.S.A.P.