Home » Magic 8-ball in Python

Magic 8-ball in Python

The Magic 8-Ball is a plastic sphere, made to look like an eight-ball, that is used for fortune-telling or seeking advice

There are 20 possible answers inside a standard Magic 8 Ball , these are as follows

● It is certain.
● It is decidedly so.
● Without a doubt.
● Yes – definitely.
● You may rely on it.
● As I see it, yes.
● Most likely.
● Outlook good.
● Yes.
● Signs point to yes.
● Reply hazy, try again.
● Ask again later.
● Better not tell you now.
● Cannot predict now.
● Concentrate and ask again.
● Don’t count on it.
● My reply is no.
● My sources say no.
● Outlook not so good.
● Very doubtful.

Ten of the possible answers are positive, five are non-committal , and five are negative.

You can read more about the magic 8-ball at https://en.wikipedia.org/wiki/Magic_8-Ball

Python code

 

import sys
import random

answer = True

while answer:
    question = input("Ask the magic 8 ball a question: (press enter to quit) ")
    
    answers = random.randint(1,20)
    
    if question == "":
        sys.exit()
    
    elif answers == 1:
        print ("It is certain")
    
    elif answers == 2:
        print ("It is decidedly so.")
    
    elif answers == 3:
        print ("Without a doubt.")
    
    elif answers == 4:
        print ("Yes - definitely.")
    
    elif answers == 5:
        print ("You may rely on it.")
    
    elif answers == 6:
        print ("As I see it, yes.")
    
    elif answers == 7:
        print ("Most likely.")
    
    elif answers == 8:
        print ("Outlook good.")

    elif answers == 9:
        print ("Yes.")

    elif answers == 10:
        print ("Signs point to yes.")
    
    elif answers == 11:
        print ("Reply hazy, try again.")
    
    elif answers == 12:
        print ("Ask again later.")
    
    elif answers == 13:
        print ("Better not tell you now.")
    
    elif answers == 14:
        print ("Cannot predict now.")
    
    elif answers == 15:
        print ("Concentrate and ask again.")
    
    elif answers == 16:
        print ("Don't count on it.")
    
    elif answers == 17:
        print ("My reply is no.")	

    elif answers == 18:
        print ("My sources say no.")
    
    elif answers == 19:
        print ("Outlook not so good.")
    
    elif answers == 20:
        print ("Very doubtful.")

 

Output

Several trial runs, enter a question or press enter to quit

8 ball output

8 ball output

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More