The Magic 8 Ball is a toy game for fortune-telling or seeking advice. In this article, we will implement the magic 8-ball game in Python.
What is the Magic 8-Ball Game?
A magic 8-ball is a hollow sphere made out of plastic and printed to look like the 8-ball from a billiards game. It is filled with a dark blue or black liquid, usually alcohol, and contains a 20-sided die. Instead of numbers on the die, the sides are printed with a variety of yes, no, and maybe-type answers. A standard Magic 8 Ball has twenty possible answers, including ten affirmative answers, five non-committal answers, and five negative answers.
To play the magic 8-ball game, the player holds the ball and thinks of a yes or no question. They then shake the ball, which agitates a printed die floating in the dark blue liquid inside of the ball. When the shaking stops, the die settles to the bottom of the ball, revealing a yes, no, or vague answer. You can read more about this game on its wiki page.
The core functionality of the magic 8-ball game is to predict the outcome of a certain event from 20 possible outcomes in a random manner. To implement this functionality, we can use the random module in Python.
How to Implement Magic 8-ball Game in Python?
We can implement the magic 8-ball game in Python using the randint()
function with if-else statements. Alternatively, we can also use the choice()
function defined in the random module in Python to implement the magic 8-ball game. Let us discuss both approaches one by one.
Using The randint() Function
To implement the magic 8-ball game in Python using the randint()
function, we will use the following steps.
- First, we will ask the user to enter a question using the
input()
function. - Next, we will generate a random number between 1 to 20 using the
randint()
function. For this, we will pass the value 1 as its first input argument and the value 20 as its second input argument. - After generating the random number, we will select one of the possible answers using the if-else block and print it.
- Until the user keeps entering questions, we will continue the game using a while loop. If the user doesn’t input any questions, we will terminate the loop using the break statement.
You can observe the above process in the following example.
import random
while True:
question=input("Ask the magic 8 ball a question: (press enter to quit) ")
answer=random.randint(1,20)
if question=="":
print("Stopping the Game.")
break
if answer==1:
print("It is certain.")
elif answer==2:
print("It is decidedly so.")
elif answer==3:
print("Without a doubt.")
elif answer==4:
print("Yes definitely.")
elif answer==5:
print("You may rely on it.")
elif answer==6:
print("As I see it, yes.")
elif answer==7:
print("Most likely.")
elif answer==8:
print("Outlook good.")
elif answer==9:
print("Yes.")
elif answer==10:
print("Signs point to yes.")
elif answer==11:
print("Reply hazy, try again.")
elif answer==12:
print("Ask again later.")
elif answer==13:
print("Better not tell you now.")
elif answer==14:
print("Cannot predict now.")
elif answer==15:
print("Concentrate and ask again.")
elif answer==16:
print("Don't count on it.")
elif answer==17:
print("My reply is no.")
elif answer==18:
print("My sources say no.")
elif answer==19:
print("Outlook not so good.")
elif answer==20:
print("Very doubtful.")
Output:
Ask the magic 8 ball a question: (press enter to quit) Python is awesome
Yes.
Ask the magic 8 ball a question: (press enter to quit) PFB is great.
Yes definitely.
Ask the magic 8 ball a question: (press enter to quit)
Stopping the Game.
In the above code, we have used 20 if-else blocks to implement the answers. You can observe that the if-else blocks made the code redundantly large. Hence, we can use a Python dictionary to imitate the functionality of the if-else blocks. For this, we will use the numbers as the keys and the answers for each number as the corresponding values.
After predicting a number using the randint()
method, we can get the answer from the dictionary and print it as shown below.
import random
answer_dict={1:"It is certain.",
2:"It is decidedly so.",
3: "Without a doubt.",
4:"Yes definitely.",
5:"You may rely on it.",
6:"As I see it, yes.",
7:"Most likely.",
8:"Outlook good.",
9:"Yes.",
10:"Signs point to yes.",
11:"Reply hazy, try again.",
12:"Ask again later.",
13:"Better not tell you now.",
14:"Cannot predict now.",
15:"Concentrate and ask again.",
16:"Don't count on it.",
17:"My reply is no.",
18:"My sources say no.",
19:"Outlook not so good.",
20:"Very doubtful."}
while True:
question=input("Ask the magic 8 ball a question: (press enter to quit) ")
answer=random.randint(1,20)
if question=="":
print("Stopping the Game.")
break
answer_string=answer_dict[answer]
print(answer_string)
Output:
Ask the magic 8 ball a question: (press enter to quit) Python is awesome.
Yes definitely.
Ask the magic 8 ball a question: (press enter to quit) PFB is great.
Reply hazy, try again.
Stopping the Game.
In this example, we first generate a random number between 1 to 20 using the randint()
function to predict the answer. As we have already defined the dictionary with numbers as keys and the possible answers as their values, we use the indexing operator to get the string value associated with the random number and print it.
Using The choice() Function
Instead of the randint()
function, we can also use the choice()
function defined in the random module to implement the magic 8-ball game. The choice()
function takes a list as its input argument and returns a random element from the list. To implement the game, we will put all 20 answers in a list. After this, whenever the user asks a question, we will select a value from the list using the choice function and print it as shown in the following example.
import random
answer_list=['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.']
while True:
question=input("Ask the magic 8 ball a question: (press enter to quit) ")
if question=="":
print("Stopping the Game.")
break
answer_string=random.choice(answer_list)
print(answer_string)
Output:
Ask the magic 8 ball a question: (press enter to quit) Python is awesome
You may rely on it.
Ask the magic 8 ball a question: (press enter to quit) PFB is great
You may rely on it.
Ask the magic 8 ball a question: (press enter to quit) PFB is awesome.
My reply is no.
Ask the magic 8 ball a question: (press enter to quit) PFB is good.
Yes definitely.
Ask the magic 8 ball a question: (press enter to quit)
Stopping the Game.
In this example, we have stored all the possible answers in a list. When the user asks for an answer, we directly select a random answer from the list using the choice()
function and print it.
Conclusion
In this article, we discussed three ways to implement the magic 8-ball game in Python. To learn more about Python programming, you can read this article on rolling the dice game in Python. You might also like this article on python trees.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.