How To Build a Simple Magic 8 Ball in Python

A step-by-step beginner’s guide

Benjamin Soyka
Nov 11 · 4 min read
Photo: Denisse Alarcon

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 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 , but you can name it whatever you’d like as long as it ends in .


Coding

Now that you have a Python file ready, we need to import the 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 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.

The function chooses a random item from a list, which is the list, in this case. Then, we use an f-string and the function to show the answer to the user.


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 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.


Possible Changes

The code in this tutorial is yours to change however you want. Here are a few examples:

Endless generating

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 loop runs, it checks if is equal to . 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 . 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 . 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!


Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Benjamin Soyka

Written by

A technology-loving student writing whenever they feel like it and coding whenever they don’t. See more at https://bsoyka.github.io

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade