Use Python to build a Dice Roller App

A simple step-by-step guide to building a Dice Rolling Simulator

John Kundycki
Towards Data Science

--

I recently applied for a Data Science job and one of the requirements to submit the application was for me to copy and paste code into a small Indeed window… Something about this process seemed a little demeaning but I took it as an opportunity to learn and to share my process.

The prompt requested that I build a simple dice roller using whatever coding language I desired and I decided I would go with Python since it is beginner friendly. The IDE I’m using is Jupyter Notebook.
In an effort to learn, I decided to take the project a little bit further by introducing a Graphical User Interface (GUI) element that allows users to pick the number of dice as well as the number of sides for the dice.

It’s a fun and simple little project so I decided to share how I went about it.

Creating the function

To create a functional dice roller, I imported 2 libraries: statistics and randint (from random). The statistics library is optional and not needed for this project, but, I think it’s neat to use the library to gather statistics on any rolling you decide to do.

from random import randint
import statistics

Now we are ready to create our dice rolling function. For this function, there will be 2 required inputs: n and x.

n will be the number of sides for the dice you are rolling.
x will be the number of dice you are rolling.

# Define the dice rolling function using two inputs.
rolls = []
def roll_many(n, x):
for i in range(x):
roll = randint(1,n)
rolls.append(roll)
print(roll)

That’s it! Simple enough. Now you can use this function to obtain dice rolls.

# This cell will simulate rolling 2 six-sided dice.
rolls = []
roll_many(6,2)

Here is an example of what should show up when you run it:

And, as previously stated, you can use the statistics library to gather statistics on your dice rolls.

statistics.mean(rolls)

Here is an example of how you can use the statistics library to get statistics on your dice rolls:

Creating a GUI for the function

I had never tried to make my Python Code work in a GUI, so this part was new to me. After a little Google searching, I decided to use the tkinter library for this part of the project.

from tkinter import *

Because this part was newer to me, I decide to exclude rolling multiple dice and kept the rolling to a singular die for simplicity.

The next code snippet might seem a little convoluted for beginners. Here I am defining a window class. In the class, I am defining the different things that will appear in the window: an area to type in the number of sides on the die, a button to roll the die and all of the labels that designate what these areas are to the user by using text.

class MyWindow:
def __init__(self, win):
self.lbl1=Label(win, text='# of Die Sides')
self.lbl2=Label(win, text='Roll Result')
self.lbl3=Label(win, text='Dice Rolling Simulator', font=("Helvetica", 20))
self.t1=Entry()
self.t2=Entry()
self.btn1 = Button(win, text='Roll Dice')
self.lbl1.place(x=100, y=100)
self.t1.place(x=200, y=100)
self.b1=Button(win, text='Roll Dice', font=("Helvetica", 16), command=self.roll)
self.b1.place(x=200, y=140)
self.b1.config(height=1, width=8)
self.lbl2.place(x=100, y=200)
self.t2.place(x=200, y=200)
self.lbl3.place(x=100, y=35)

Next, I define the roll function for the button. The roll function here, is very similar to the one we saw above, however there are a few additional lines so that the GUI will utilize the function.

def roll(self):
self.t2.delete(0, 'end')
n=int(self.t1.get())
result=randint(1,n)
self.t2.insert(END, str(result))

Finally, I define the window using tkinter, I add a title (the name title of the window that appears in the top part) to the window as well as the dimensions and the position on the screen the window will appear. The window.mainloop() is an event listening loop, so that our app is ready to respond to new input.

window=Tk()
mywin=MyWindow(window)
window.title('Dice Roller')
window.geometry("400x300+10+10")
window.mainloop()

Altogether, the snippet looks like this:

from tkinter import *
class MyWindow:
def __init__(self, win):
self.lbl1=Label(win, text='# of Die Sides')
self.lbl2=Label(win, text='Roll Result')
self.lbl3=Label(win, text='Dice Rolling Simulator', font=("Helvetica", 20))
self.t1=Entry()
self.t2=Entry()
self.btn1 = Button(win, text='Roll Dice')
self.lbl1.place(x=100, y=100)
self.t1.place(x=200, y=100)
self.b1=Button(win, text='Roll Dice', font=("Helvetica", 16), command=self.roll)
self.b1.place(x=200, y=140)
self.b1.config(height=1, width=8)
self.lbl2.place(x=100, y=200)
self.t2.place(x=200, y=200)
self.lbl3.place(x=100, y=35)
def roll(self):
self.t2.delete(0, 'end')
n=int(self.t1.get())
result=randint(1,n)
self.t2.insert(END, str(result))
window=Tk()
mywin=MyWindow(window)
window.title('Dice Roller')
window.geometry("400x300+10+10")
window.mainloop()

And, after running the code…

Voilà! Let’s input a 20 for the # of Die Sides and see what we get:

Lucky number seven! It’s nothing fancy, but it’s a working Dice Rolling Simulator.

If you are having problems writing the code or just want to copy and paste the code so you have the Dice Rolling Simulator, here is the GitHub repository link:

Best of luck to you and thanks for reading!

--

--