Python for Kids! Design a Rock, Paper, Scissors a​nd Fortune Teller Game

This post may contain affiliate links.

Sharing is caring!

Welcome to our python for kids tutorial!

Are you looking to teach your kids python? You’ve come to the right place. This is part TWO of our Python for kids tutorials. If you are looking for a SUPER SIMPLE introduction to Python check out part ONE of our learn python tutorial for kids here.

What is Python?

What is python? A snake? A big heavy snake? Yes, actually, a python is one of the biggest snakes that exists in this world. But the word ‘python’ is also famous for something else. Yes, if you come from a programming background, then you may have heard this word. In the programming world, the word ‘python’ does not means a big snake, but it surely reflects something big.

Python is a high-level programming language that is well known to programmers. It is one of the most popular and widely used programming languages worldwide. Python is one of the easiest programming languages to learn for kids. In this tutorial, we will learn a few basic concepts of python programming language with the help of some fun games you will create in python.

A brief history of the Python programming language

But first of all, why would you name a programming 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.

Today, Python is one of the most popular programming languages. It is used in web application development, client-side, server-side development, machine learning, Scientific and Numeric Computing, and many more areas. But what makes python so successful? Let’s discuss a few of its features briefly.

Python Coding for Kids – Key Concepts

1. Syntax:

Syntax is the spelling and grammar of programming languages. One of the best features of python is its intuitive and easy to read syntax. This makes it much easier for beginners to learn python. As compared to other programming languages such as Java, C/C++, Javascript, python’s syntax is much simpler. It is much more error-free. 

2. Third-party modules:

Modules are like code libraries that give us access functions we may want to include in our program. Third-party modules are always useful when it comes to development. The Python Package Index (PyPI) contains a wide range of python third-party modules that are capable of interacting with other languages and platforms.

3. Libraries:

What is a library used for? A library is mostly used for providing external features that are not supported by a programming language or framework. It is always efficient to use libraries to provide extended functionalities. Python supports an extensive amount of libraries for functionalities such as internet protocols, web services tools, string operations, and operating system interfaces.

4. Strong community:

A beginner may wonder why community matters for a programming language! Python is an open-source programming language, that means anyone can contribute to it. Python’s strong community shares knowledge with each others, works on issues and resolve bugs. Python’s community is very strong and also really helpful for beginners!

5. User-friendly Data Structures:

Data structures are a bit of an advanced area for beginners but it is one of the most important parts of a programming language. These are very useful while programming and are used commonly in the programming world. Data structures in python are very easy and user-friendly. Unlike other programming languages, python’s data structures are easy to learn.

Python Coding Game #1: Rock, Paper, Scissors

Rock, Paper and Scissors. Are you familiar with these three words being spoken at the same time? I think you are. Rock, Paper, and Scissors is a popular game. My brother and I used to play rock, paper, scissors to decide who would get to choose the morning cartoons before school!

Rock, paper scissors is played between two people, both of them choosing one out of the three options. Then, according to certain rules, one of the two is declared the winner. We will discuss these rules further and moreover, we will implement Rock, Paper, and Scissors in the python programming language.

Nervous? Well, no need to get nervous because it is not as tough it sounds! As I mentioned earlier, Python is one of the easiest programming languages and it does not take much time to understand it. So let’s start.

Python Rock Paper Scissors Worksheets

The details of our tutorial below have been compiled in an easy to follow fillable worksheet aimed at teaching students Python.

Our worksheet set includes:

  • 15 page fillable worksheets and step by step programming guide
  • Teachers guide with answers
  • Students will create a working Rock, Paper, Scissors game in Python

Let’s decompose Rock, Paper, Scissors

Before moving to the coding part, the first rule of programming and development is always to analyze and break down what we are going to implement. Breaking down our problems into smaller steps is called decomposition. Here, we are going to create a program for the game, Rock, Paper, and Scissors. Let’s divide the problem into parts.

1. The players. Remember, we need two people in this game. For this program, we will have one user and the other will be the computer. Both the user and computer are going to enter their inputs. Don’t worry about the computer’s input, it is going to be the most interesting part.

2. The rules. Next, we have to create example scenarios to decide who is the winner of each turn. Let’s discuss these scenarios briefly:

– It is a tie when both the user and the computer makes the same choice.

– Rock wins over Scissors and loses to Paper

– Paper wins over Rock and loses to Scissors

– Scissors wins over Paper and loses to Rock

3. Exit strategy. There will always be an option to finish the game.

4. Who won? We also need to keep the count of points earned by both. When individuals play rock, paper, scissors they often make it a best of 3 or best of 5 game!

So, now we know what we are going to do. Let’s get coding!

Coding a Python Game: Step-By-Step Instructions

I have created a fully working code for Rock, Paper and Scissors. You can find the full code at the end of this post. Let’s discuss it step by step. I would suggest going through each line of code together in your classroom and discuss it as you go. At the end, students will have a fully working Rock, Paper, Scissors game that they can play against the computer!

This game was simply created using variables, list, while loop, if-elif-else ladder and a special randint method that we imported from random module. We will review all of these beginner coding concepts below!

If you don’t have a python editor and are looking for a really simple way to get started with Python right away, you can use an online Python IDE editor.  Simply open up this page,  https://repl.it/languages/python3, and you will be ready to get started right away!

Step 1: Importing modules

from random import randint

The very first line of the code is: from random import randint.

What does this line actually mean? Read it and you might get an idea. We are importing randint function from the random module using ‘from’ and ‘import’ keywords. The randint function has some special abilities that we are going to use in our program. We will discuss it later. Now that we have important this function we can use it in our code.

Step 2: Creating a list of available options

Earlier we discussed data structures. As I mentioned earlier, data structures in python are user-friendly and very easy to understand. Let’s look at our next line of code for our first data structure, a list.

#List of options
game  = ["Rock", "Paper", "Scissors"]

Observe the above line of code. We created a list data structure and named it, ‘game’. Then we initialized it with the three available choices we have, Rock, Paper, and Scissors.

Remember, there are also few other data structures in python and each of them has different functionalities. A list is created by using square brackets. To retrieve its values, all we need to do is, write the name of the list, followed by two square brackets, and then enter the index of the value we wish to retrieve.

If you want to learn more about lists, check out our beginner python tutorial for kids where we review this topic in detail.

Suppose we wish to print the second item on the list. How will we do this? Simple! we will use its index and remember, the index always starts from 0. This means the second item on the list has the first index. Observe the syntax below.

game[1]

Step 3: Getting the computer to choose randomly

So we already imported the randint function and we also know how to retrieve values from a list. Now its time to combine both these concepts we learned to get the choice for the computer. Yes, you read it right. Observe the following code. This is how the computer is making its choices randomly in this program.

#Assigning a random option to computer
computer = game[randint(0,2)]

Pay attention now! We created a variable named computer and this variable will hold the computer’s choice. We already have the list of choices and we need to think of something so that the computer can choose one of it randomly. Randomly? Yes! this is where the randint function will come in play.

The randint function is used to generate a random value. The lower and upper limits are passed to this function and the returned value is always between this range. Here, we will pass 0 and 2 because of our list, the game has three values. Don’t forget that 0 is considered the first value. Therefore 0, 1, and 2, make three options total!

randint(0,2)

This piece of code can return 0, 1 or 2. It can be unique or the same. It just depends upon the python’s mood! Joking 🙂 It is totally random! We are going to use the returned number as the index of the list. 

game[randint(0,2)]

We write the whole randint function inside the square brackets, so the returned random number can work as the index. Now, the computer’s choice is made and the variable, the computer holds it. We will use this line of code again later, stay tuned!

Step 4: Let’s define a few more variables before we start our game

Variables are an important part of programming. We review the basics of variables and their importance of coding in part one our Python guide for kids. Check it out here!

For our Rock, Paper, Scissors game, there are a few other variables to consider. Let’s discuss them

playersPoint = 0
computersPoint = 0

These two variables will keep the count of points for the user and computer. This is why there are initialized to zero.

goOn = True

Remember, I mentioned that along with options, the user can also finish the game. The above variable will end the game when user types ‘Finish’. The goOn variable is set to True and it will be a condition of the while loop. Don’t worry! we will discuss the while loop later.

When the user enters ‘Finish’, the value of this variable will be changed to false and the game will end.

Step 5: Creating a While loop

Next let’s move on to the working area of our program. We used a while loop, but why? What is this while loop doing here? The while loop allows our game to run until the game is ended by the user. Observe the line that contains the while loop.

#Loop goes on until goOn is false
while(goOn):

In the parenthesis, goOn variable is used. Remember, the goOn variable has True as its value. This means, the condition in while loop is true and it will run until the condition is false. And when will the condition becomes false? Exactly when the goOn variable’s value is changed to false. Later on, we will tell our program to change the goOn variable to false when the user types “Finish”

Step 6: Allowing User input

The first line of code within the while loop is: 

#Ask for user input<br>
    player = input("Rock, Paper or Scissors? or enter Finish to end!\n")

This is where we are asking the user for their choice. The four options are Rock, Paper, Scissors, and Finish. The user has to enter one of these or the program will not go further. To get input from the user, the input function is used.

The choice is stored in the player variable. The text written inside the function appears on the screen. Remember, we have to enter the values exactly as asked! This is uppercase and lowercase sensitive!

Step 7: Define the Scenarios

We have two variables: computer and player, holding choices of the computer and user respectively.

Now it is time to decide the winner. We will do it by using if-elif-else ladder. Have a look at the ladder we used in the program and then we will discuss it.

  #Check for scenarios
    if(player == 'Finish'):
        goOn = False
    elif(player == computer):
        print("Tie!")
    elif(player == "Rock"):
        if(computer == "Paper"):
            print("You lose!", computer, "covers", player)
            computersPoint = computersPoint + 1 
        else:
            print("You win!", player, "smashes", computer)
            playersPoint = playersPoint + 1
    elif(player == "Paper"):
        if(computer == "Scissors"):
            print("You lose!", computer, "cut", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "covers", computer)
            playersPoint = playersPoint + 1
    elif(player == "Scissors"):
        if(computer == "Rock"):
            print("You lose...", computer, "smashes", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "cut", computer)
            playersPoint = playersPoint + 1
    else:
        print("That's not a valid play. Check your spelling!")

Let’s understand how the ‘if-elif-else ladder’ works. Each if and elif has a condition part in the parenthesis. The program will enter that block whose condition is true. Once the true condition is found, it will ignore all other conditions. But what happens when none of the conditions is true? The execution goes into the else block. Observe the above code once more, the else block has no condition.

We will discuss all the conditions step by step.

1. The first condition checks whether the user entered ‘Finish’ or not. If this condition is true, the value of the goOn variable will be changed to false and the program will end.

   if(player == 'Finish'):
        goOn = False

2. The second condition checks where the choice of both the user and computer is the same. It this condition is true, no points are awarded to anyone.

elif(player == computer):
        print("Tie!")

3. The next three conditions work according to the basic concepts of the game. The winner is decided and points are awarded accordingly. Earlier, we initialized two variables with 0, computersPoint and playersPoint. If the computer wins, computersPoint variable is incremented by 1 and if the user wins, playersPoint variable is incremented by 1.

elif(player == "Rock"):
        if(computer == "Paper"):
            print("You lose!", computer, "covers", player)
            computersPoint = computersPoint + 1 
        else:
            print("You win!", player, "smashes", computer)
            playersPoint = playersPoint + 1
    elif(player == "Paper"):
        if(computer == "Scissors"):
            print("You lose!", computer, "cut", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "covers", computer)
            playersPoint = playersPoint + 1
    elif(player == "Scissors"):
        if(computer == "Rock"):
            print("You lose...", computer, "smashes", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "cut", computer)
            playersPoint = playersPoint + 1

4. Last is the else block. There is no condition. It only displays a message when the user has entered an invalid choice.

    else:
        print("That's not a valid play. Check your spelling!")

Step 8: Allowing the Game to Continue

At the end of the while loop, we used the randint function once again to assign a choice for the computer.

#Assigning a random option to computer
    computer = game[randint(0,2)]
    print('********Next Turn********')

We have to repeat this even though we had a similar line of code at the beginning of our game. Remember, in every turn, the computer has to make a choice. This is why we write this line of code again.

Step 9: Displaying the final score

When the user ends the game by typing ‘Finish’, the program will display the final score. The final score is stored in the two variables we used earlier, computersPoint and playersPoint.

#Printing final points
print("********Final Points********")
print("Player: ", playersPoint)
print("Computer: ", computersPoint)

Rock Paper Scissors – Full Code

There is the full code you will need to run a Rock, Paper, Scissors game in Python. You can copy and paste this code into https://repl.it/languages/python3 and play this game for yourself!

from random import randint

#List of options
game  = ["Rock", "Paper", "Scissors"]

#Assigning a random option to computer
computer = game[randint(0,2)]


#Keep count for points
playersPoint = 0
computersPoint = 0

goOn = True

#Loop goes on until goOn is false
while(goOn):
    #Ask for user input
    player = input("Rock, Paper or Scissors? or enter Finish to end!\n")

    #Check for scenarios
    if(player == 'Finish'):
        goOn = False
    elif(player == computer):
        print("Tie!")
    elif(player == "Rock"):
        if(computer == "Paper"):
            print("You lose!", computer, "covers", player)
            computersPoint = computersPoint + 1 
        else:
            print("You win!", player, "smashes", computer)
            playersPoint = playersPoint + 1
    elif(player == "Paper"):
        if(computer == "Scissors"):
            print("You lose!", computer, "cut", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "covers", computer)
            playersPoint = playersPoint + 1
    elif(player == "Scissors"):
        if(computer == "Rock"):
            print("You lose...", computer, "smashes", player)
            computersPoint = computersPoint + 1
        else:
            print("You win!", player, "cut", computer)
            playersPoint = playersPoint + 1
    else:
        print("That's not a valid play. Check your spelling!")
    #Assigning a random option to computer
    computer = game[randint(0,2)]
    print('********Next Turn********')

#Printing final points
print("********Final Points********")
print("Player: ", playersPoint)
print("Computer: ", computersPoint)

Let’s look at the final code in action!

A user can enter ‘Rock’,’Paper’, or ‘Scissors’ to play, or ‘finish’ to end the game. Then the value is matched with the computer’s choice and the points are distributed accordingly. When the game is ended, final points are displayed on the screen.

So this is how we can create a simple Rock, Paper and Scissors game in python.

Python Coding Game #2: 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-elif-else ladder and a special randint method that we imported from random module. In this tutorial, we are going to create another game that mimics a class toy from my childhood: The Magic 8 ball – fortune teller. This will be fun!

We will use the basic concepts we learned last time and combine them with another new concept of programming language that is known as functions.

All About Functions

Functions are an integral part of almost every programming language. Functions exist in python too. What are functions and why we need them? A function is an organized block of code that is defined for some specific task.

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 programming. 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.

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 programming. 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 randint method. Last time we similarly used the randint method to pick a choice for computer from the array containing three values – Rock, Paper and Scissors. This time will do something similar to this. Let’s see what we are going to do in this game.

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 randint method will return a random number from 0 to 19.

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!

Pin for Later!

Enjoyed our tutorial? Pin for later!

This awesome Python tutorial for kids is perfect for kids in middle school and high school. We will review how to make a functioning 'rock, paper, scissors game' that you can play against the computer. This python for kids tutorial will also teach how to program a magic 8 ball. We will review basic coding concepts in this in dept Python coding tutorial. #coding #education
These python games are so much fun! Teach python with these simple python tutorials for kids. Kids will learn to program their own rock paper scissors and magic 8 ball games with this step by step python programming tutorial for the classroom. Enjoy playing these fun computer coding games and learn the basic concepts of coding as well. #coding #education #technology

Similar Posts