Editor

Python Program for a Magic 8 Ball

Beginner · Games and random numbers

A Magic 8 Ball is a toy that gives a random answer to a yes or no question, such as "It is certain" or "Ask again later". This program keeps the answers in a list and picks one at random for each question.

Program

magic_8_ball.py
import random
answers = [
"It is certain.",
"Without a doubt.",
"Yes, definitely.",
"Most likely.",
"Ask again later.",
"Cannot predict now.",
"Don't count on it.",
"My sources say no.",
"Very doubtful.",
]
questions = [
"Will I pass my exam?",
"Will it rain tomorrow?",
"Should I learn Python?",
]
# To ask your own: question = input("Ask the Magic 8 Ball a question: ")
for question in questions:
print("Question:", question)
print("Magic 8 Ball says:", random.choice(answers))
print()
Output
Question: Will I pass my exam?
Magic 8 Ball says: Without a doubt.

Question: Will it rain tomorrow?
Magic 8 Ball says: Without a doubt.

Question: Should I learn Python?
Magic 8 Ball says: Without a doubt.

How it works

  1. The possible answers are stored in a list: some say yes, some say no and some are vague.
  2. random.choice(answers) picks one at random for each question, so the answers change on every run.
  3. The question itself does not affect the answer, which is how the real toy works too.
  4. The questions list stands in for questions the user would type, as shown in the input() comment.

Other ways to write it

Asking until the user types quit

Asking until the user types quit
import random
answers = ["Yes.", "No.", "Maybe.", "Ask again later.", "Definitely!"]
typed = ["Is Python fun?", "Will I win the match?", "quit", "Never asked"]
# or: question = input("Your question (or quit): ") inside a while True loop
for question in typed:
if question.lower() == "quit":
print("Goodbye!")
break
print(question, "->", random.choice(answers))
Output
Is Python fun? -> Maybe.
Will I win the match? -> Definitely!
Goodbye!

The loop stops at "quit", so the last question is never asked. In the interactive version a while True loop keeps asking until the user types quit, and break ends it.

Some answers more likely than others

Some answers more likely than others
import random
answers = ["Yes", "No", "Ask again later"]
weights = [5, 3, 2] # "Yes" is the most likely
for i in range(1, 6):
pick = random.choices(answers, weights=weights)[0]
print(f"Answer {i}: {pick}")
Output
Answer 1: Yes
Answer 2: No
Answer 3: Ask again later
Answer 4: Ask again later
Answer 5: Ask again later

random.choices() with weights makes some answers come up more often, here "Yes" about half the time. It returns a list, so [0] takes the single answer out of it.

Try it yourself

Add three answers of your own, and print "That is not a question!" when a question does not end with "?".

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises