Python Dice Rolling Simulator Program for Beginners

Python-Dice-Rolling-Simulator-Program-for-Beginners
Credit: codingstreets

Overview: Python Dice Rolling

Let’s get started with the Python Dice Rolling Simulator Tutorial for Beginners. Here we will explore a complete Python Tutorial for how to code Python Dice Simulator program.

What’s Next? — Python Dice Rolling

In this article, we will explore Master Python Basics By Building a Dice Rolling Simulator. Along with this, you will learn various Python concepts like: random module, time module, Python while loop, Python conditional statement. So, ready for Your First Python Project: A Dice Rolling Simulator (Step-by-Step Tutorial)?

Table of Contents

Watch Video Explanation

Pre-requisite for the project

Key Points to Remember for Logic Building

Complete Python Dice Rolling Simulator Code

				
					# Dice Rolling Simulator Program

#LOGIC BUILDING

#1. random numbers (1-6)
#2. conditional statement
#3. loop

import random
import time

# Dice faces using ASCII art
DICE_FACES = {
    1: (
        "┌─────────┐",
        "│         │",
        "│    ●    │",
        "│         │",
        "└─────────┘"
    ),
    2: (
        "┌─────────┐",
        "│ ●       │",
        "│         │",
        "│       ● │",
        "└─────────┘"
    ),
    3: (
        "┌─────────┐",
        "│ ●       │",
        "│    ●    │",
        "│       ● │",
        "└─────────┘"
    ),
    4: (
        "┌─────────┐",
        "│ ●     ● │",
        "│         │",
        "│ ●     ● │",
        "└─────────┘"
    ),
    5: (
        "┌─────────┐",
        "│ ●     ● │",
        "│    ●    │",
        "│ ●     ● │",
        "└─────────┘"
    ),
    6: (
        "┌─────────┐",
        "│ ●     ● │",
        "│ ●     ● │",
        "│ ●     ● │",
        "└─────────┘"
    )
}

def dice_roll():
    return random.randint(1,6)

def dice_simulator():
    print("🎲 Welcome to the Dice Rolling Simulator! 🎲")
    print("Type 'yes' to roll the dice or 'no' to quit.")
    print()

    while True:
        ask_user = input("Your input: ").lower().strip()

        if ask_user == "yes":
            for _ in range(3):
                print("🎲", end=" ")
                time.sleep(0.5)
            print()

            result = dice_roll()
            print(f"👉 You rolled: {result}")
            for line in DICE_FACES[result]:
                print(line)
            print()

        elif ask_user == "no":
            print("Thanks for playing! Goodbye!👋")
            break
        else:
            print("❌ Please type 'yes' to roll the dice or 'no' to quit.")
            print()

if __name__ == "__main__":
    dice_simulator()
				
			

Step by Step Code Explanation

				
					import random
import time
				
			

Explanation: Import necessary Python modules. Random module to generate random numbers. time module is used to decide the display time of animation for rolling dice.

				
					DICE_FACES = {
    1: (
        "┌─────────┐",
        "│         │",
        "│    ●    │",
        "│         │",
        "└─────────┘"
    ),
    2: (
        "┌─────────┐",
        "│ ●       │",
        "│         │",
        "│       ● │",
        "└─────────┘"
    ),
    3: (
        "┌─────────┐",
        "│ ●       │",
        "│    ●    │",
        "│       ● │",
        "└─────────┘"
    ),
    4: (
        "┌─────────┐",
        "│ ●     ● │",
        "│         │",
        "│ ●     ● │",
        "└─────────┘"
    ),
    5: (
        "┌─────────┐",
        "│ ●     ● │",
        "│    ●    │",
        "│ ●     ● │",
        "└─────────┘"
    ),
    6: (
        "┌─────────┐",
        "│ ●     ● │",
        "│ ●     ● │",
        "│ ●     ● │",
        "└─────────┘"
    )
}
				
			

Explanation: Created dice face using ASCII Art. Defined a dictionary and stored the dice number as key and dice face as value of the dictionary. There are a total 6 faces of a dice; therefore there are 6 pairs of key:values, one pair for each face.

				
					def dice_roll():
    return random.randint(1,6)
				
			

Explanation: Created a function dice_roll that generates the random numbers using a random module. Used the randint method to specify the range of numbers, i.e.,1 to 6 for dice rolling.

				
					def dice_simulator():
    print("🎲 Welcome to the Dice Rolling Simulator! 🎲")
    print("Type 'yes' to roll the dice or 'no' to quit.")
    print()
				
			

Explanation: Created another function dice_simulator. Used print() function to display the necessary statements.

				
					while True:
     ask_user = input("Your input: ").lower().strip()
				
			

Explanation: Defined a Python while loop to keep the dice rolling until the user selects to stop rolling the dice. The function input() used to ask from the user whether to start the dice rolling or not. The method lower() and strip() is used to convert the user’s input to lower case and remove unwanted whitespaces respectively.

				
					if ask_user == "yes":
   for _ in range(3):
        print("🎲", end=" ")
        time.sleep(0.5)
   print()
				
			

Explanation: Defined condition if user gives input as yes. Loop 3 times using for loop to display the dice icon in a single line using the end parameter. time module with sleep method is used to delay the animation of the dice icon.

				
					result = dice_roll()
print(f"👉 You rolled: {result}")
for line in DICE_FACES[result]:
    print(line)
print()
				
			

Explanation: The function dice_roll() is called to generate the random number within a range of 1 to 6. The print() function is used to display the dice rolled number. The for loop is used to access the dice face according to the dice number generated.

				
					elif ask_user == "no":
   print("Thanks for playing! Goodbye!👋")
   break
				
			

Explanation: The elif condition defined if the user gives input as no. In this case, dice rolling stops and by using break keyword Python jumps out of the while loop. The break keyword is used to stop the program because all conditional statements are written inside the while loop.

				
					else:
    print("❌ Please type 'yes' to roll the dice or 'no' to quit.")
    print()
				
			

Explanation: The else condition defined if the user gives input neither yes nor no. The user gives the wrong input. So, a statement is displayed guiding the user to pass the right input.

				
					if __name__ == "__main__":
    dice_simulator()
				
			

Explanation: The line → if __name__ == “__main__”: denotes that program is run from the current file where the actual dice roll program is written. Finally, the function is called to execute the whole program.

Now it’s Your Turn!

Challenge: Turn this into a ‘Guessing Game.’ The user has to guess the dice roll before it happens! Who’s up for the challenge? Let me see your ideas 💡Post your answers in YouTube comment section I Built a Python Dice Simulator in 20 Minutes! video.

Conclusion: Python Dice Rolling Simulator

Finally, we have just written a complete Python Dice Rolling Simulator Tutorial for Beginners. In this Python beginner project, we discussed how to use Python common modules, methods, loop, conditional statements, etc. Overall, this was an engaging and useful Python beginner project of 2025 in which in just a couple of lines we finished with First Python Project: A Dice Rolling Simulator (Step-by-Step Tutorial).

Recent Articles