“Pig” is an easy and fun dice game that anyone in the family can play. You only need one die in hand and know basic arithmetic to play the game. It’s an entertaining way for kids to practice adding and learn about probability.

📌 [Download] Free Python Lesson Plans Get free 1-week Python lesson plans and slides for kids ages 11-13 to start learning about Python coding. Download Now

If there’s no one around to play with, you can challenge the computer! Build your own “Pig” game using Python that you can play anytime. This simple app will roll the dice for you and the computer and add up your points too. All you have to do is click.

Complete this Python coding tutorial to make your own “Pig” Dice Game.

Pig dice game

This is a beginner Python tutorial that will walk you through all of the code from start to finish.

Play the completed “Pig” Dice Game

What you need:

1. Text editor

We’ll be using the CodeWizardsHQ editor to write and run our Python code. If you’re a CodeWizardsHQ student, download the x_hour_of_code_2022 project for the completed code. 

You can also use an online text editor like replit that allows you to author and run Python programs in a web browser.

2. An empty Python file

Create a new empty Python file and add your code there. 

Our file is named main.py. If you’re using another platform to write and execute your code, you can choose whichever name you like.

This tutorial is for beginner Python programmers ages 11+. It assumes you understand basic Python code. Let’s get started!

Step 1: Create and display a welcome message with the game rules

We’ll begin by displaying a welcome message to the user that explains the rules of “Pig”, the dice game we’ll build in this project.



welcome_message = """
          Welcome to 'Pig', a dice game!
   
    In this game, a user and a computer opponent
    roll a 6-sided die each round. If the value of
    the die is a 1, the player that rolled the 1 loses
    all of their points. Otherwise, the player gets the
    value of the die added to their points. The first
    player to reach 30 points wins!
"""
welcome_message = """
        ...
"""

print(welcome_message)

Step 1 Output:

Python tutorial step 1 output

Step 2: Get the user’s name and create the game loop

Prompt the user for their name, create a game loop, and allow the user to roll the dice.

welcome_message = """
        ...
"""

print(welcome_message)

username = input("What is your name? ")
username = input("What is your name? ")

while True:
    input(f"Press 'Enter' to roll the die {username}!\n")
while True:
    input(f"Press 'Enter' to roll the die {username}!\n")

    break

Hint: Ask any question you like and get the input from the user. The \n notation adds an additional new line which makes our output look a little nicer.

Step 2 Output:

Python tutorial step 2 output

Step 3: Simulate rolling dice

Simulate rolling a six-sided die and display the player’s and computer’s roll.

from random import randint


welcome_message = """
    ...
"""
while True:
    input(f"Press 'Enter' to roll the die {username}!\n")

    player_die_value = randint(1, 6)
    print(f"{username} rolls a {player_die_value}")

    break
while True:
    input(f"Press 'Enter' to roll the die {username}!\n")

    player_die_value = randint(1, 6)
    print(f"{username} rolls a {player_die_value}")

    computer_die_value = randint(1, 6)
    print(f"Computer rolls a {computer_die_value}")

    break

Step 3 Output:

Python tutorial step 3 output

Step 4: Create a function to update the scores of each player

Now, let’s calculate and track the player’s and computer’s score after each roll. We’ll create a function to determine whether each player gets points added to their score or gets their score reset to 0.

player_score = 0
computer_score = 0

welcome_message = """
    ...
"""
from random import randint


def update_score(score, die_value):



player_score = 0
computer_score = 0
def update_score(score, die_value):

    if die_value == 1:
        return 0
    else:
        return score + die_value

while True:
    ...

    computer_die_value = randint(1, 6)
    print(f"Computer rolls a {computer_die_value}")

    player_score = update_score(player_score, player_die_value)
    computer_score = update_score(computer_score, computer_die_value)

    break
while True:
    ...

    player_score = update_score(player_score, player_die_value)
    computer_score = update_score(computer_score, computer_die_value)

    print(f"{player_score=}")
    print(f"{computer_score=}")

    break

Step 4 Output:

Python tutorial step 4 output

Step 5: Display a formatted scoreboard

In this step, we’ll make our game feel a bit more realistic by creating and displaying a scoreboard each round that holds the current score.

def update_score(score, die_value):
    ...


def display_scoreboard(player_score, computer_score):
def display_scoreboard(player_score, computer_score):

    print()
    print("#" * 20)
    print(f"Player Score: {player_score}")
    print(f"Computer Score: {computer_score}")
    print("#" * 20)
    print()
while True:
    ...

    player_score = update_score(player_score, player_die_value)
    computer_score = update_score(computer_score, computer_die_value)

    print(f"{player_score=}")    # Remove this!
    print(f"{computer_score=}")  # Remove this!

    break
while True:
    ...

    player_score = update_score(player_score, player_die_value)
    computer_score = update_score(computer_score, computer_die_value)

    display_scoreboard(player_score, computer_score)

    break

Hint: You can create multiple copies of a string in Python using the star (*) operator, which is a handy tool for building our scoreboard banner.

Step 5 Output:

Python tutorial step 5 output

Step 6: Determine the game winner

In the final step, we’ll check the scores of each player at the end of our game loop and determine who wins! The first player to reach 30 points is the winner.

while True:
    ...

    player_score = update_score(player_score, player_die_value)
    computer_score = update_score(computer_score, computer_die_value)

    display_scoreboard(player_score, computer_score)

    break  # Remove this!
while True:
    ...

    display_scoreboard(player_score, computer_score)

    if player_score >= 30:
        print(f"{username} wins!")
        break
    elif computer_score >= 30:
        print("Computer wins!")
        break

Step 6 Output:

Python tutorial step 6 output

Hint: We didn’t handle ties in this version, but challenge yourself to do that with another “if” statement.

Your game is complete!

Check out the finished project.

Complete python tutorial for kids

Download the project files and open main.py to view the completed project.

We’ve built a simple version together, but there’s plenty more you can do to customize and upgrade your version of “Pig”. 

Download 1-Week Python Lesson Plans

Kds ages 11-13 can start learning Python in a structured way. Download a FREE 1-week lesson plan with activities and slides. Enter your name and email to receive the free lesson plans in your inbox today.

If you want to build more games and apps in Python, join CodeWizardsHQ’s live coding classes for kids. It’s the most fun and effective way for kids to learn Python and advance to a real-world coding internship. 

Students in our middle school and high school core track start by learning fundamental coding concepts in Python. They work with a live, expert instructor who supports them every step of the way. Classes are engaging and you’ll build personalized projects and applications in every lesson. 

Enjoy this project!