DEV Community

Cover image for Build a Simple Guessing Game in Python
Ben Soyka
Ben Soyka

Posted on • Originally published at betterprogramming.pub

Build a Simple Guessing Game in Python

In this piece, you will learn how to make a simple high-low guessing game in Python.


Concept

The user will guess a number. If their guess is correct, they win. If it is not correct, the program will tell them to guess higher or lower depending on the number. This will repeat until the user has won.


Getting Started With Python 3

First of all, if you don’t already have Python installed, you will need to install it on your computer. You can do this from the Python website. You will need the latest version of Python 3 for this tutorial. (version 3.x.x)

Make sure you check the box that says to add Python to the PATH variable. If you don’t do this, it will be difficult to run the program.

Now, open up a text/code editor on your device. Personally, I use Brackets. Windows comes with Notepad pre-installed. Mac OS includes TextEdit. Linux users can use Vim.

Once you have a text editor open, save a new file. I would name it main.py, but you can call it whatever you’d like, as long as it ends in .py.


Coding

The instructions for this tutorial will be included in the code itself as comments. In Python, a comment starts with a # and continues until the end of the line.

# First, we need to import the 'random' module.
# This module contains the functionality we need to be able to randomly select the winning number.

import random

# Now, we need to select a random number.
# This line will set the variable 'correct' to be equal to a random integer between 1 and 10.

correct = random.randint(1, 10)

# Let's get the user's first guess using the 'input' function.

guess = input("Enter your guess: ")

# Right now, the user's input is formatted as a string.
# We can format it as an integer using the 'int' function.

guess = int(guess)

# Let's start a loop that will continue until the user has guessed correctly.
# We can use the '!=' operator to mean 'not equal'.

while guess != correct:

    # Everything in this loop will repeat until the user has guessed correctly.
    # Let's start by giving the user feedback on their guess. We can do this using the 'if' statement.

    # This statement will check if a comparison is true.
    # If it is, the code inside the 'if' statement will run.

    if guess > correct:

        # This code will run if the user guessed too high.
        # We can show a message to the user using the 'print' function.

        print("You've guessed too high. Try guessing lower.")

    else:

        # The 'else' statement adds on to an 'if' statement.
        # It will run if the condition of the 'if' statement is false.

        # In this case, it will run if the user guessed too low, so we can give them feedback.

        print("You've guessed too low. Try guessing higher.")

    # Now we need to let the user guess again.
    # Notice how I am combining the two lines of guessing code to make just one line.

    guess = int(input("Enter your guess: "))

# If a user's guess is still incorrect, the code in the 'while' loop will be repeated.
# If they've reached this point in the code, it means they guessed correctly, so let's say that.

print("Congratulations! You've guessed correctly.")
Enter fullscreen mode Exit fullscreen mode

Also, feel free to change whatever you’d like to in your program.

For example, you could make the correct number be between 1 and 100 instead of 1 and 10. You could change what the program says in the print() functions. Whatever you want, it’s your code now.


Running Your Program

Open up the command prompt (Windows/Linux) or the terminal (Mac), depending on your operating system. Try each of the following commands in order. At least one of them should work if Python is installed correctly.

python C:/Users/username/Desktop/main.py
py C:/Users/username/Desktop/main.py
python3 C:/Users/username/Desktop/main.py
Enter fullscreen mode Exit fullscreen mode

Make sure you replace C:/Users/username/Desktop/main.py with the full path to your Python file.

Once your program is running, test it out! Play around with it a few times. You can run it again once it is done by pressing the up arrow key to copy your last command, then pressing Enter.

If you have any questions about this tutorial, please let me know and I will try to help you out. Below is a version of the code without any comments.

import random

correct = random.randint(1, 10)

guess = input("Enter your guess: ")
guess = int(guess)

while guess != correct:
    if guess > correct:
        print("You've guessed too high. Try guessing lower.")
    else:
        print("You've guessed too low. Try guessing higher.")

    guess = int(input("Enter your guess: "))

print("Congratulations! You've guessed correctly.")
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (0)