# Dice Simulator using Python

In this article, we will make a dice simulator. For this we will use 'time' and 'random' module. 'random' module will help us generate a random number between 1 and 6 (both inclusive) and 'time' module will help us with extra effects while simulating a dice.

We will not just output a number between 1 and 6, rather we will print a dice using the print() method. Next, we will ask the user if you want to roll the dice again and will roll again or stop based on the user's input.

Save the below code in a file named 'dice.py'.

import random
import time

x =
"y"

while x == "y":
no = random.randint(
1, 6)
print(
"Rolling...\n")
time.sleep(
3)

if no == 1:
print(
"Dice:")
print(
"[ ]")
print(
"[ 0 ]")
print(
"[ ]")

if no == 2:
print(
"Dice:")
print(
"[0 ]")
print(
"[ ]")
print(
"[ 0]")

if no == 3:
print(
"Dice:")
print(
"[ ]")
print(
"[0 0 0]")
print(
"[ ]")

if no == 4:
print(
"Dice:")
print(
"[0 0]")
print(
"[ ]")
print(
"[0 0]")

if no == 5:
print(
"Dice:")
print(
"[0 0]")
print(
"[ 0 ]")
print(
"[0 0]")

if no == 6:
print(
"Dice:")
print(
"[0 0 0]")
print(
"[ ]")
print(
"[0 0 0]")

x = input(
"\nPress 'y' to roll again: ")
print(
"\n")

The code is simple. We are working in a while loop in which we first generate a random integer between 1 and 6. Then we print "Rolling%u2026" and delay for 3 seconds for extra effects. Then, we print the dice based on the random number previously generated.

After printing the dice we ask the user if they to roll again and exit the while loop if they input anything other than 'y' else we follow the procedure above again.

To run the simulator, enter the below command in a Terminal/Command Line:

python3 dice.py

A sample output looks like this

Rolling...

Dice:

[0   0]

[     ]

[0   0]

Press 'y' to roll again: y

Rolling...

Dice:

[0    ]

[     ]

[    0]

Press 'y' to roll again: y

Rolling...

Dice:

[0    ]

[     ]

[    0]

Press 'y' to roll again: n

#### More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1027 2
Hangman Game using Python 16786 2
Using Databases with CherryPy application 1673 2
nose: Working 507 2
pytest: Working 511 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 509 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1397 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1302 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2997 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2