Python Magic 8 Ball
Learn how to make a magic 8 ball game in python with this free python web tutorial
Creating a “Magic 8 ball game using Python
In this introduction to programming using Python, you will create a “Magic 8-Ball” game. The game will work by asking the user to input a yes / no style question and will respond with one of it’s classic predictions such as: “Yes”, “Most likely” and “Outlook not so good”.
In this introduction to programming using Python, you will create a “Magic 8-Ball” game. The game will work by asking the user to input a yes / no style question and will respond with one of it’s classic predictions such as: “Yes”, “Most likely” and “Outlook not so good”.
Introduction:
The Magic 8-ball is a fortune telling toy created by Mattel in the 1950s.
The Magic 8-ball is a fortune telling toy created by Mattel in the 1950s.
Image by CRASH:Candy, used under a Creative Commons Attribution 2.0 Generic license (Flickr image link)
The concept is simple. Ask the 8-ball a yes or no question and the 8-ball will reply with answers such as “Yes”, “No”, “Without a doubt” etc. (Seemingly being able to predict the future)
The Magic 8 Ball is made up of 20 responses – 10 positive, 5 negative and 5 neutral. The 20 answers are:
Positive 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
Negative answers:
● Don't count on it ● My reply is no ● My sources say no ● Outlook not so good ● Very doubtful
Neutral answers:
● Reply hazy try again ● Ask again later ● Better not tell you now ● Cannot predict now ● Concentrate and ask again
According to Wikipedia: (http://en.wikipedia.org/wiki/Magic_8-Ball) “Using the coupon collector's problem in probability theory, it can be shown that it takes, on average, 72 outcomes of the Magic 8 Ball for all 20 of its answers to appear at least once.”
To find out more about the Magic 8-Ball game, click on the links below:
The Magic 8 Ball is made up of 20 responses – 10 positive, 5 negative and 5 neutral. The 20 answers are:
Positive 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
Negative answers:
● Don't count on it ● My reply is no ● My sources say no ● Outlook not so good ● Very doubtful
Neutral answers:
● Reply hazy try again ● Ask again later ● Better not tell you now ● Cannot predict now ● Concentrate and ask again
According to Wikipedia: (http://en.wikipedia.org/wiki/Magic_8-Ball) “Using the coupon collector's problem in probability theory, it can be shown that it takes, on average, 72 outcomes of the Magic 8 Ball for all 20 of its answers to appear at least once.”
To find out more about the Magic 8-Ball game, click on the links below:
- Wikipedia (Magic 8-Ball) - http://en.wikipedia.org/wiki/Magic_8-Ball
- Magic 8-Ball.org - http://www.magic8ball.org
STEP 1: Introducing the Random Function
Before you create your Magic 8-ball game, you need to become familiar with the random function in Python.
Let's start by writing an algorithm to simulate the flipping of a coin.
Type in and run the following code (using the code window above):
Before you create your Magic 8-ball game, you need to become familiar with the random function in Python.
Let's start by writing an algorithm to simulate the flipping of a coin.
Type in and run the following code (using the code window above):
import random
cointoss = [“Heads”,”Tails”] print(random.choice(cointoss)) |
Use the code window (above) to test out your code!
How the code works!
The first line imports the random function needed for the 'coin toss' game to work. The second line of code creates a list containing our two possible outcomes from flipping a coin ('Heads' or 'Tales') and that the last line selects a random entry from the list (in this case either “Heads” or “Tails”) and display it on the screen.
Challenge 1
Comment the code, using the hashtag (#), explaining what each line of code is doing. Note: these three lines of code will form the main basis of your ‘Magic 8-ball’ game.
Challenge 2
The first line imports the random function needed for the 'coin toss' game to work. The second line of code creates a list containing our two possible outcomes from flipping a coin ('Heads' or 'Tales') and that the last line selects a random entry from the list (in this case either “Heads” or “Tails”) and display it on the screen.
Challenge 1
Comment the code, using the hashtag (#), explaining what each line of code is doing. Note: these three lines of code will form the main basis of your ‘Magic 8-ball’ game.
Challenge 2
- Modify your code to simulate the rolling of a dice.
STEP 2 – Creating a simple Magic 8-Ball game
Use the code window above to test your 'magic 8 ball'.
Let's start by importing the necessary libraries to make our code work. Enter the folowing code (in the code window above):
import random
import time |
When creating your Magic 8-Ball game, you will first need to create a list containing all of the 8-ball's possible responses. I suggest starting with just 3 responses: "Yes". "No" and "Maybe".
Next, let's create a list containing some responses. For example:
Next, let's create a list containing some responses. For example:
responses = ["yes","no","maybe"]
|
Next, let's add some code to instruct the user to input a 'Yes/No answer' type question:
question = input("Please ask me a Yes/No style question ")
|
Next, let's add in a pause (using the time.sleep() function) to make it appear as if the computer is thinking about the answer:
time.sleep(2)
|
Finally, let's add the code to select a random response from the list:
print(random.choice(responses))
|
Challenge 3
- Comment your code explaining what each line is doing.
- Input all of the 20 classic answers. (See above) Hint: You can change the answers if you wish however, you must keep to the format of 10 positive, 5 negative and 5 neutral answers.
- Modify the code so that the pause is random.
- Modify your code so that the game repeats (loops) until the user tells it to stop.
Resources:
Example solution
|
|
Challenge 4
Having really long lists in Python is not very efficient. Not only that, it makes it more difficult to debug. A much better way to handle the list of responses would be to use a text file.
In the code window below, we have added a text file called 'responses.txt'
In the text file, add all the remaing 20 possible responses (each on a new line). Note: The first two have been done for you!
Having really long lists in Python is not very efficient. Not only that, it makes it more difficult to debug. A much better way to handle the list of responses would be to use a text file.
In the code window below, we have added a text file called 'responses.txt'
In the text file, add all the remaing 20 possible responses (each on a new line). Note: The first two have been done for you!
It is certain
It is decidedly so ... |
Finally, enter the following code in the code window above (making sure that you read the comments explaining what the code is doing).
import random
import time # Open the 8 Ball responses text file f = open('responses.txt','r') # Read the whole file and store each line in a list responses = f.readlines() # Ask the user to input a Yes/No style question question = input("Please ask me a question") # Pause for 2 seconds to make it look like the computer is thinking time.sleep(2) # Print a response at random from the list print(random.choice(responses)) |
Extra Challenge
- Comment your code explaining what each line is doing.
- Input all of the 20 classic answers. Remember: You can change the answers if you wish however, you must keep to the format of 10 positive, 5 negative and 5 neutral answers.
- Modify the code so that the pause is random.
- Modify your code so that the game repeats (loops) until the user tells it to stop.
- Modify your code so that it becomes a random name picker for your class.
Python 'Magic 8-Ball' lesson is licenced under a Creative Commons Attribution 4.0 International License.
You may also be interested in:
Tags: Python web, python programming language, python programming, python coding