Magic 8 Ball Program in Python



The Magic 8 Ball was a toy that could produce usually yes or no responses to various questions and as such is good for making decisions or killing time. Here, let us discuss on the way to develop a basic Python program that mimics the workings of the Magic 8 Ball.

This will involve teaching the learner on how to handle the user input, the capability of the program to generate responses as well as embrace sheer basic decision making for improved use of the program.

What is Magic 8 Ball Program?

The Magic 8 Ball program is an application that allows the user to type in a question and enters a pre-determined list of possible responses where after the program selects a random response and presents it to the user. The program also contain logic to answer sensitive questions in a way that is non communitive. This makes the program more refined to the peak of making it fit any user in the market without directly offending any party.

Steps to Build the Magic 8 Ball Program

1. Setting Up Your Environment

Python is installed on your system. Use any text editor or IDE like PyCharm, Visual Studio Code, or Python IDLE to write and run your code.

2. Import the Required Module

The random module is important for this program as it allows us to randomly select a response from our list of answers.

Importing random module

import random

3. Define the Function

Create a function called magic_8_ball() that will contain the core logic of the program.

4. Create a List of Responses

The responses list contains many type of answers that the Magic 8 Ball can provide.

CODE:  responses = [
   "Yes, definitely.",
   "As I see it, yes.",
   "Reply hazy, try again.",
   "Cannot predict now.",
   "Do not count on it.",
   "My sources say no.",
   "Outlook not so good.",
   "Very doubtful."
]

5. Program Structure

The program is structured to continuously prompt the user to enter a question. After each question, it provides a random answer from the list and asks if the user wants to continue.

if __name__ == "__main__":
   while True:
      magic_8_ball()
      play_again = input("Do you want to ask another question? (yes/no): ").strip().lower()
      if play_again != 'yes':
         print("Goodbye!")
         break

Implementation of Magic 8 Ball Program

import random
def magic_8_ball():
   responses = [
      "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."
   ]

   neutral_responses = [
      "The future is uncertain.",
      "Only time will tell.",
      "That's a mystery even to me!",
      "Some things are best left unknown.",
      "Focus on the present!"
   ]

   sensitive_keywords = ["destroy", "end", "die", "death", "apocalypse", "war", "kill"]

   question = input("Ask the Magic 8 Ball a question: ").lower()

   # Check for sensitive keywords
   if any(word in question for word in sensitive_keywords):
      answer = random.choice(neutral_responses)
   else:
      answer = random.choice(responses)

   print("Shaking the Magic 8 Ball...")
   print("The Magic 8 Ball says: " + answer)

if __name__ == "__main__":
   while True:
      magic_8_ball()
      play_again = input("Do you want to ask another question? (yes/no): ").strip().lower()
      if play_again != 'yes':
         print("Goodbye!")
         break

Output

Magic 8 Ball
python_projects_from_basic_to_advanced.htm
Advertisements