Build a Simple Guessing Game in Python
Tags: beginners, programming, tutorial, python
Published: 2022-02-11 04:18:36

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](https://www.python.org/downloads/). 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](https://brackets.io/). Windows comes with Notepad pre-installed. Mac OS includes TextEdit. Linux users can use [Vim](https://www.vim.org/).

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.

```py
# 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.")
```

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.

```sh
python C:/Users/username/Desktop/main.py
py C:/Users/username/Desktop/main.py
python3 C:/Users/username/Desktop/main.py
```

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.

```py
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.")
```

---

## Resources

* [Download Python](https://www.python.org/downloads/)
* [Brackets Code Editor](https://brackets.io/)
* [GitHub gist containing main code](https://gist.github.com/bsoyka/e2a3361d3a5691f3fce80694b2543574)
* [GitHub gist containing commands](https://gist.github.com/bsoyka/72c8091ad2ef171a362916c826adacb8)
* [GitHub gist containing main code without comments](https://gist.github.com/bsoyka/a253dd91974d2b91010c85064480e4cb)