Python Magic 8 Ball Fortune Teller Game
This post may contain affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Let’s get started learning to code a Magic 8 Ball fortune teller game in Python. Today we’ll be showing you how to code this game and reviewing the important coding concepts covered. If you are looking for an introduction to the basics of Python, check out our beginner tutorial.
Python? What’s that?
Today,
But first of all, why would you name a 撩起白丝校花的短裙进入 language python? Well, when Guido van Rossum, the creator of Python was implementing the code for the language, he used to read scripts from Monty Python’s Flying Circus TV series. I guess this came in his mind as he named the language Python! Although python first appeared in 1990, it took some time to become popular.
Python Coding Game: Create a Magic 8 Ball Fortune Teller
Last time, we built a simple Rock, Paper and Scissors game in python. The game was simply created using variables, list, while loop, if-
We will use the basic concepts we learned last time and combine them with another new concept of 撩起白丝校花的短裙进入 language that is known as functions.
All About Functions
Functions are an integral part of almost every 撩起白丝校花的短裙进入 language. Functions exist in python too. What are
Functions are used for code reusability. When we need a single block of code multiple times in a program, should we write it every time again? Absolutely not! This is where functions come in play. Functions can be called from anywhere in python. Values can be passed as parameters to a function and values can be returned from a function as well.
Functions are very helpful in 撩起白丝校花的短裙进入. We will focus on functions in the magic 8 ball game. But first, let’s discuss a few advantages of using functions.
Advantages of functions in coding
1. Ease in program development.
2. Testing becomes easy.
3. Code reusability.
4. Better program readability.
5. Sharing of code becomes possible.
Python Fortune Teller Digital Resource
Do you want all of the material covered in this tutorial in an easy to use in classroom lesson? Grab our Python Fortune Teller Tutorial!

- 巜催情精油按摩2高潮 Python tutorial for teachers to introduce their students to Python.
- Includes a 13-page PDF worksheet with an answer guide and a 30-slide Google Slides presentation.
- Covers how to program a Fortune Telling game in Python.
- PDF worksheet contains exercises that gradually develop students’ 撩起白丝校花的短裙进入 skills.
- Google Slides presentation is engaging and visually appealing, with interactive examples and illustrations.
- Suitable for both experienced and new 撩起白丝校花的短裙进入 instructors.
- Provides a solid foundation in Python 撩起白丝校花的短裙进入 for students to build on.
- An excellent resource to teach Python in a fun and effective way.
Want all our Python Tutorials?
- The Python Ultimate Bundle is an all-in-one package that includes all the Python tutorials you need to teach your students 撩起白丝校花的短裙进入 and game development.
- The bundle includes tutorials on Python Basics, Python Lists, creating a story in Python, Rock Paper Scissors game, Fortune Teller game, Create Your Own Adventure game, Blackjack game, and Dice game.
- Each tutorial is engaging, fun, and easy to follow with clear instructions and real-world examples.
- The bundle includes resources such as PDF 欠cao的sao货撅屁股双性, answer guides, and Google Slides presentations to help students learn at their own pace.
- This bundle is perfect for teachers who want to provide their students with a 巜催情精油按摩2高潮 introduction to Python 撩起白丝校花的短裙进入 and game development.
What is the Magic 8 Ball?

What is magic 8 ball? It is basically a toy that was developed in the 1950s. The user asks a question and a reply appears on the surface of the ball. If a user asks, “Am I going to die today?”, the reply could be, “Certainly yes!”. Don’t worry! You can play this game because there is no truth in its replies. It just randomly picks an answer from a set of answers and displays it on the screen whenever input is received.
So it is basic 撩起白丝校花的短裙进入. What comes in your mind when I say “It just randomly picks an answer from a set of answers”? Yes! you guessed it right, the
Let’s Decompose the Magic 8 Ball Game!
1. Again, there are two parties involved. One is the user and second is the magic 8 ball.
2. The user will type a question and magic 8 ball will give a reply.
3. There will be a set of answers and one answer per question will be picked from this set.
4. There will always be an option to finish the game.
Magic 8 Ball – Full Working Code
So here is the full working code of Magic 8 ball – fortune teller game in python. We will discuss this code step by step but first, make sure, you go through it properly.
#importing randint method
from random import randint
#List of answers
answers = ['Outlook good', 'Yes Signs point to yes', 'Reply hazy', 'try again', 'Ask again later', 'Better not tell you now','It is certain', 'It is decidedly so', 'Without a doubt', 'Yes – definitely', 'You may rely on it', 'As I see it, yes', 'Most likely', 'Cannot predict now', 'Concentrate and ask again', 'Dont count on it', 'My reply is no', 'My sources say no', 'Outlook not so good', 'Very doubtful']
print('Hello stranger!, I am the Magic 8 Ball')
print('**********')
#The magic 8 ball function
def Magic8Ball():
print('Ask me a question.')
input()
#using randint method
print (answers[randint(0, len(answers)-1)] )
print('I hope that helped!')
#calling Replay function
choice = Replay()
if(choice == 'Y'):
#Calling Magic8ball function
Magic8Ball()
elif(choice == 'N'):
return
else:
print('I did not understand! Please repeat.')
#Calling Replay function
Replay()
#Function for user's decision
def Replay():
print ('Do you have another question? Enter Y if yes or N if no.')
choice = input()
#Returning user input
return choice
#Calling Magic8ball function
Magic8Ball()
print('*****I hope you got your answers*****')
Importing the randint method
#importing randint method from random import randint
Here we imported the randint method from random module. We will use this method for choosing answers later.
List of answers
#List of answers answers = ['Outlook good', 'Yes Signs point to yes', 'Reply hazy', 'try again', 'Ask again later', 'Better not tell you now','It is certain', 'It is decidedly so', 'Without a doubt', 'Yes – definitely', 'You may rely on it', 'As I see it, yes', 'Most likely', 'Cannot predict now', 'Concentrate and ask again', 'Dont count on it', 'My reply is no', 'My sources say no', 'Outlook not so good', 'Very doubtful']
This is quite a big list! There are 20 answers on this list. Magic 8 ball can choose any answer from this list. You can also personalize this to your own set of answers! Get creative!
Calling the Magic8Ball function
Further, there is a simple printing part and two functions. These two functions won’t do anything until they are called. So let’s skip this part for the time being and jump directly to the ending part of the code.
Magic8Ball()
This is where everything begins. One of the two functions is named Magic8Ball and here we are calling it. The program execution will skip the two function and when this line get’s executed, the magic8ball function will get invoked. There are no parameters for the magic8ball function. So there is nothing passed in the parenthesis.
Working of Magic8Ball function
Now let’s understand the working of the Magic8Ball function.
def Magic8Ball():
print('Ask me a question.')
input()
#using randint method
print (answers[randint(0, len(answers)-1)] )
print('I hope that helped!')
#calling Replay function
choice = Replay()
if(choice == 'Y'):
#Calling Magic8ball function
Magic8Ball()
elif(choice == 'N'):
return
else:
print('I did not understand! Please repeat.')
#Calling Replay function
Replay()
Pay attention to the very first line of the above code – def Magic8Ball(). This is how functions are defined in python using the def keyword. First, we ask for the user’s questions. Here, we are not storing the user’s question anywhere because it does not matter what the user asks. This is the concept of the game.
Pay attention to the next line.
print(answers[randint(0, len(answers)-1)] )
This is the most important part of the game – Answer from Magic8Ball. So what is happening here? The randint method is used to get a random number.
randint(0, len(answers)-1)
Last time in the Rock, Paper, and Scissors game, we did something similar to this. There we passed 0 and 2 in the randint method because there were only 3 values to choose from. But in this program, we have 20 or we can have 30 or as much as answers we want. So instead of using a static value, we used a method that counts the length of the list.
len(answers)
This will return 20 because there are 20 values in the answers list. But remember the index of a list always starts from 0, which means, the last element is on the 19th index. You can learn more about this method of counting in our basic Python Tutorial. That is why we subtracted 1 from the returned value. Now the
answers[randint(0, len(answers)-1)]
The randint method is used in the square brackets. This is how a random answer will appear.
The Replay() function
This is the part where the user decides, should the game continue or not. We have a separate function for this input – Replay().
def Replay():
print ('Do you have another question? Enter Y if yes or N if no.')
choice = input()
#Returning user input
return choice
As mentioned earlier, each function has a specific task. The magic8ball function asks the question and gives an answer. The replay function asks what does the user want to do next – play or quit. The user has to enter ‘Y’ for yes and ‘N’ for no.
The return keyword is used to return a value from a function. But we need to store this returned value. But wait a minute! where did we call this replay function? In the magic8ball function. Let’s go back there.
Calling the replay function in magic8ball function
In the magic8ball function, we call the Replay function and store the returned value in the choice variable.
choice = Replay()
The choice variable will be used next in the if-elif-else ladder.
The if-elif-else Ladder
if(choice == 'Y'):
#Calling Magic8ball function
Magic8Ball()
elif(choice == 'N'):
return
else:
print('I did not understand! Please repeat.')
#Calling Replay function
Replay()
Let’s understand the if-elif-else ladder step by step.
1. If the value of the choice variable is ‘Y’, the magic8ball function will be called again.
2. If the value of the choice variable is ‘N’, the program will exit the magic8ball function. Now pay attention here.
elif(choice == 'N'):
return
We only used the return keyword but no value is returned with it. This is a way of exiting from a function.
3. If the value of the choice variable is anything else except ‘Y’ and ‘N’, The reply function will be called again for appropriate user input.
This is how we can build a simple Magic 8 ball – fortune teller game in python.
Let’s look at the final code in action!
When the code is run the computer will start by asking the use a question. The user enters a question and the computer will randomly generate an answer, just like the toy magic 8 ball!
Python Fortune Teller Digital Resource
Do you want all of the material covered in this tutorial in an easy to use in classroom lesson? Grab our Python Fortune Teller Tutorial!

- 巜催情精油按摩2高潮 Python tutorial for teachers to introduce their students to Python.
- Includes a 13-page PDF worksheet with an answer guide and a 30-slide Google Slides presentation.
- Covers how to program a Fortune Telling game in Python.
- PDF worksheet contains exercises that gradually develop students’ 撩起白丝校花的短裙进入 skills.
- Google Slides presentation is engaging and visually appealing, with interactive examples and illustrations.
- Suitable for both experienced and new 撩起白丝校花的短裙进入 instructors.
- Provides a solid foundation in Python 撩起白丝校花的短裙进入 for students to build on.
- An excellent resource to teach Python in a fun and effective way.
Want all our Python Tutorials?
- The Python Ultimate Bundle is an all-in-one package that includes all the Python tutorials you need to teach your students 撩起白丝校花的短裙进入 and game development.
- The bundle includes tutorials on Python Basics, Python Lists, creating a story in Python, Rock Paper Scissors game, Fortune Teller game, Create Your Own Adventure game, Blackjack game, and Dice game.
- Each tutorial is engaging, fun, and easy to follow with clear instructions and real-world examples.
- The bundle includes resources such as PDF 欠cao的sao货撅屁股双性, answer guides, and Google Slides presentations to help students learn at their own pace.
- This bundle is perfect for teachers who want to provide their students with a 巜催情精油按摩2高潮 introduction to Python 撩起白丝校花的短裙进入 and game development.

Nazanin Azimi is a technical writer with several years of experience in the software industry. She is currently a senior at Carnegie Mellon studying computational physics and creative writing. She has been head teaching assistant for Principles of Computing, an introductory python and computer science course at CMU. She has held several technical internships including software engineering intern at Mathworks. Outside of technical writing, Nazanin also enjoys writing and studying poetry.

