Magic 8 balls are so refreshingly retro that when I saw this snippet of code I just had to give it a spin. So here you go – a Python-infused magic 8 ball ready to answer all of life’s most important questions 🙃

About the Magic 🎱 Ball Program

When run, this program randomly generates a magic 8 ball style response.

Just like with the original, you’ll have to ask the question in your head (or out loud – depending on who’s around).

Original code from here.

How to use it

First thing’s first – open Python’s IDLE or Anaconda’s Spyder. If you don’t have at least one of them installed you can find instructions to install Python here or Anaconda here.

Once open, type or paste the following code into the editor and save it as Alternatively, you can download the file here.

import random
def getAnswer(answerNumber):
    if answerNumber == 1:
        return 'It is certain'
    elif answerNumber == 2:
        return 'It is decidedly so'
    elif answerNumber == 3:
        return 'Yes'
    elif answerNumber == 4:
        return 'Reply hazy try again'
    elif answerNumber == 5:
        return 'Ask again later'
    elif answerNumber == 6:
        return 'Concentrate and ask again'
    elif answerNumber == 7:
        return 'My reply is no'
    elif answerNumber == 8:
        return 'Outlook not so good'
    elif answerNumber == 9:
        return 'Very doubtful'

r = random.randint(1,9)
fortune = getAnswer(r)

Once saved, press F5 to run the program.

If you’re using IDLE, your magic 8 ball results will appear in a popup screen. If you’re using Spyder you can find your result in the console, on the right-hand side of your screen.

Press F5 every time you want to ‘shake’ the magic 8 ball and get a new response to those most pressing of questions that just need answering.

By tweaking the code below you can make any type of Python-driven fortune teller you want. You can change the “answer” that the program outputs by changing the return string. Increase or decrease the number of potential responses by changing the values held by the random.randint() function.

A good way to tweak this project and better understand the code is to change the return string from magic 8 ball answers to fortune cookie responses 🥠 (no question needed in this case).

How it works

If you’re brand new to Python, check out this Absolute Beginner’s Guide to Python which outlines the most basic elements of Python.

In a nutshell: This program assigns a number to 9 different responses, or ‘fortunes’, such as “reply hazy, try again” or “it is decidedly so”.

It then selects a random number between 1 and 9, using Python’s random.randint() function. Once the number is selected, the program looks back to see which fortune is assigned to the number chosen and lets you know what fate has in store.

The details

Looking for more detail? Let’s break this down:

  • Firstly, the Magic 8 Ball program imports Python’s random module.
  • Next, it defines the getAnswer() function.
  • Then the random.randint() function is called and told to select a number between 1 and 9.
  • This value is stored in the variable named r, which in turn is stored in a parameter named answerNumber.
  • Depending on the number in the answerNumber parameter, the getAnswer() function returns one of the nine different string values we provided.
  • The returned string is assigned to the variable called fortune which the print() function then prints onto the screen.

Boom. Fortune told.