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 magic8ball.py. 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) print(fortune)
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, thegetAnswer()
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.