Rolling Dice Python
Rolling Dice Python
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
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 1/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
about the whole process seemed demeaning but I went through with it.
Get started Open in app
The prompt requested that I build a simple dice roller using whatever coding language I
desired. Since Python is where I have done most of my learning, I naturally chose that.
The IDE I’m using is Jupyter Notebook.
I went a little above and beyond and made a Graphical User Interface (GUI) for the dice
roller and made it so you can pick the number of sides on the dice as well as the number
of dice you want to roll because I like to challenge myself.
It’s a fun and simple little project so I decided to share how I went about it.
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.
That’s it! Simple enough. Now you can use this function to obtain dice rolls.
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 2/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
# started
Get This cell will
Open simulate rolling 2 six-sided dice.
in app
rolls = []
roll_many(6,2)
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:
Because this part was newer to me, I decide to exclude rolling multiple dice and kept the
rolling to a singular die for simplicity.
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 3/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
The next code snippet might seem a little convoluted for beginners. Here I am defining a
Get started Open in app
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.
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 4/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
window=Tk()
Get started Open in app
mywin=MyWindow(window)
window.title('Dice Roller')
window.geometry("400x300+10+10")
window.mainloop()
window=Tk()
mywin=MyWindow(window)
window.title('Dice Roller')
window.geometry("400x300+10+10")
window.mainloop()
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 5/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
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:
jkundycki/RollDice
You can't perform that action at this time. You signed in with another
tab or window. You signed out in another tab or…
github.com
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 6/7
4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science
Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials
and cutting-edge research to original features you don't want to miss. Take a look.
Your email
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.
https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 7/7