Magic 8 Ball Program In Python

random module in Python:
The random module in Python can be used to generate random numbers.
These are pseudo-random numbers since the seed determines the sequence of numbers generated.
This module can be used to do tasks like generating random numbers, outputting a random value for a list or string, and so on.

Given a list of 8 sentences and the game is to select any random sentence out of the given 8 sentences. This program/code is also known as the magic 8 ball Program.

Python Program for Magic 8 Ball Code Implementation

Approach:

  • Import random module using the import keyword.
  • Create a function say codeMagic_8ball() (Which is used to Implement the main logic of the magic 8 ball program)
  • Give the response you want as user input using the input() function and store it in a variable.
  • Give the list of all eight ball answers and store it in another variable.
  • Check if the obtained/input response is “quit” using the if conditional statement.
  • If it is true then print Some Random Acknowledgement indicating the Game is over.
  • Else print some random answer from the above given list of answers using the choice() function of the random module by Passing the about Eight answers list to the choice() Function.
  • Here \n is used as a separator to Print the new line.
  • Call the above-created codeMagic_8ball() function Recursively to Play the Game Again.
  • Call the above created codeMagic_8ball() function to start the game.
  • The Exit of the Program.

Below is the Implementation:

# Import random module using the import keyword
import random

# Create a function say codeMagic_8ball() (Which is used to Implement the main logic of the magic 8 ball program)
def codeMagic_8ball():
    # Give the response you want as user input using the input() function and 
    # store it in a variable
    User_response = input("Enter any key for answer and 'quit' to exit:")
    # Give the list of all eight ball answers and store it in another variable
    Magic_8ball_answers = [
        "BtechGeeks platform for all type of articles",
        "Python Programs for in depth articles about Python Language",
        "SheetTips for Articles about excel and google sheets",
        "PythonArray",
        "Hello Good Morning",
        "This is BtechGeeks",
        "This is PythonPrograms",
        "Refer Excel Articles on SheetTips"
        ]
    # Check if the obtained/input response is "quit" using the if conditional statement
    if User_response == "quit":
        # If it is true then print Some Random Acknowledgement indicating the Game is over.
        print("The Game is Ended <!!>")
    else:
        # Else print some random answer from the above given list of answers using the 
        # choice() function of the random module by Passing the about Eight answers list to choice Function
        # Here \n is used a separator to Print the new line
        print(random.choice(Magic_8ball_answers), "\n")
        # Call the above created codeMagic_8ball() function Recursively to Play the Game Again.
        codeMagic_8ball()

# Call the above created codeMagic_8ball() function to start the game.
codeMagic_8ball()

Output:

Enter any key for answer and 'quit' to exit:v
This is PythonPrograms

Enter any key for answer and 'quit' to exit:r
This is PythonPrograms

Enter any key for answer and 'quit' to exit:f
Refer Excel Articles on SheetTips

Enter any key for answer and 'quit' to exit:u
Python Programs for in depth articles about Python Language

Enter any key for answer and 'quit' to exit:w
Hello Good Morning

Enter any key for answer and 'quit' to exit:k
This is PythonPrograms

Enter any key for answer and 'quit' to exit:z
Hello Good Morning

Enter any key for answer and 'quit' to exit:q
PythonArray

Enter any key for answer and 'quit' to exit:e
SheetTips for Articles about excel and google sheets

Enter any key for answer and 'quit' to exit:d
SheetTips for Articles about excel and google sheets

Enter any key for answer and 'quit' to exit:m
Python Programs for in depth articles about Python Language

Enter any key for answer and 'quit' to exit:l
Python Programs for in depth articles about Python Language

Enter any key for answer and 'quit' to exit:l
PythonArray

Enter any key for answer and 'quit' to exit:c
SheetTips for Articles about excel and google sheets

Enter any key for answer and 'quit' to exit:quit
The Game is Ended <!!>