How To Build a Simple Magic 8 Ball in Python
A step-by-step beginner’s guide
We all know what a magic 8 ball is, right? You ask it a question, shake it up, and read its (often mysterious) response.
It’s pretty easy to make one in Python, too. In this tutorial, you’ll learn exactly how, as well as some changes you can make afterward. You’ll learn some Python skills that you can easily use in other programs, too.
Getting Started With Python
First of all, if you don’t already have Python installed, you can install it from the Python website. For the code in this tutorial, you’ll need a version of Python that’s version 3.6 or greater.
When installing Python, make sure to check the box to add Python to the
PATH variable. If you don’t, it will be difficult to run the program once it’s done.
Once you’ve installed Python, open up a text/code editor on your computer. I prefer to use either Brackets or PyCharm. Windows comes with Notepad pre-installed, macOS includes TextEdit, and Linux users can use Vim.
With your text/code editor opened, save a new file. I would name it
main.py, but you can name it whatever you’d like as long as it ends in
Now that you have a Python file ready, we need to import the
random module. This module will randomize the answers in the program.
In Python, it’s conventional to have all your imports at the top of each file, and this is easy to do. Start your program with this line:
Up next, you’ll need to add in a list of possible responses for the magic 8 ball. This will be a list of strings stored in a variable named
Next, ask the user a question. The
input function returns whatever the user entered. Since this magic 8 ball is random, you don’t need to save their question at all.
Finally, you need to select a random response and show it to the user.
random.choice function chooses a random item from a list, which is the
responses list, in this case. Then, we use an f-string and the
Running Your Program
Start by opening up the command prompt (Windows/Linux) or the terminal (Mac). Try running each of the commands below in order. At least one of them should work if you’ve installed Python correctly.
Make sure you replace
C:/Users/username/Desktop/main.py with the full path to your Python file.
Once your program is running, play around with it! You can quickly run it again by pressing the up-arrow key and then pressing Enter/Return.
Here’s an example of what your terminal might look like. Keep in mind that this is on Windows, and your terminal may appear differently.
The code in this tutorial is yours to change however you want. Here are a few examples:
If you want to continuously generate new responses, you’ll have to run the program again. It’s pretty easy to make it so that the program runs in an indefinite loop.
Just put this line before asking the user for their question:
Then, indent everything after the new line with four spaces.
This works because every time the code inside the
while loop runs, it checks if
True is equal to
True. This will always be true, so the loop will run until it is manually stopped.
Try running the program again. It will keep asking for a question and giving a response.
To break out of the loop, press CMD/CTRL + C, then press Enter/Return. You’ll see an error message ending in
KeyboardInterrupt. Then, you’ll see your terminal waiting for another command.
What is the meaning of life?
When a user asks what the meaning of life is, we can tell them
42. Think of this as a sort of hidden Easter egg for the Hitchhiker’s Guide to the Galaxy lovers out there.
Take a look at this code, which includes comments. Comments in Python start with a
# and continue until the end of the line.
Just replace the normal response-printing code with this code and you’ll be good to go!
Whatever else you want
Do what you want with this code! Change parts of it, add more interesting features, or remove something. It’s all up to you!