575
The Magic 8 Ball program is a simple game that simulates the classic toy, which gives random answers to yes-or-no questions.
In this Python program, the user can ask any question, and the program will respond with a randomly chosen answer.
Code
import random def magic_8_ball(): print("Welcome to the Magic 8 Ball!") print("Ask a yes-or-no question, or type 'quit' to exit.") # List of possible answers answers = [ "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: # Prompt the user for a question question = input("What is your question? ") # Exit condition if question.lower() == "quit": print("Goodbye!") break # Choose a random answer from the list response = random.choice(answers) # Display the response print("Magic 8 Ball says:", response) # Run the game magic_8_ball()
Explanation of the Code
- Import the random Module:
- random is a standard Python module that provides functions for generating random numbers and selecting random items from a list.
- We use random.choice() to randomly select an answer from the list.
- Define the magic_8_ball Function:
- This function contains the main logic of the game.
- Introduction to the Game:
- print(“Welcome to the Magic 8 Ball!”): Greets the player.
- print(“Ask a yes-or-no question, or type ‘quit’ to exit.”): Provides instructions to the player, telling them how to quit the game.
- Define the List of Possible Answers:
- answers is a list that holds all possible responses the Magic 8 Ball can give.
- The list includes responses in various categories, such as:
- Positive: “It is certain,” “Yes, definitely,” etc.
- Uncertain: “Reply hazy, try again,” “Ask again later,” etc.
- Negative: “Don’t count on it,” “My sources say no,” etc.
- Game Loop (while True):
- This while loop continues indefinitely until the player types “quit”.
- Inside the loop:
- Prompt for a Question:
- question = input(“What is your question? “): Prompts the player to enter a yes-or-no question.
- Exit Condition:
- if question.lower() == “quit”:: Checks if the user entered “quit” (case-insensitive).
- print(“Goodbye!”): Prints a goodbye message.
- break: Exits the loop and ends the game.
- Choosing a Random Answer:
- response = random.choice(answers): Selects a random response from the answers list.
- Display the Response:
- print(“Magic 8 Ball says:”, response): Prints the chosen answer to the player.
- Prompt for a Question:
How the random.choice() Function Works
The random.choice() function selects a random item from a non-empty sequence, like a list. Here, it’s used to select a random answer from the answers list each time the user asks a question.
Sample Game Play
Here’s an example of how the program’s output might look when played:
Welcome to the Magic 8 Ball! Ask a yes-or-no question, or type 'quit' to exit. What is your question? Will I get a promotion? Magic 8 Ball says: Most likely. What is your question? Is it going to rain tomorrow? Magic 8 Ball says: Cannot predict now. What is your question? quit Goodbye!
Explanation of the Game Flow
- The game welcomes the user and provides instructions.
- The user types a question, and the program selects and displays a random response.
- The game continues, allowing the user to ask more questions until they type “quit” to end the game.
Summary
- The Magic 8 Ball program simulates a classic game, providing random answers to yes-or-no questions.
- The program uses a list to store possible responses and random.choice() to select a random answer.
- A while loop allows the game to continue until the user decides to quit.
This program is a fun example of using lists and random selection in Python to create a simple, interactive game.