Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
258 views

Rolling Dice Python

The document describes how to build a dice roller application using Python. It explains how to create a dice rolling function that takes the number of sides and number of dice as inputs. It then shows how to create a graphical user interface (GUI) for the dice roller using Tkinter that allows the user to enter the number of sides and click a button to roll the dice and see the results. The full code for the GUI dice roller application is provided.

Uploaded by

Marwan Monajjed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
258 views

Rolling Dice Python

The document describes how to build a dice roller application using Python. It explains how to create a dice rolling function that takes the number of sides and number of dice as inputs. It then shows how to create a graphical user interface (GUI) for the dice roller using Tkinter that allows the user to enter the number of sides and click a button to roll the dice and see the results. The full code for the GUI dice roller application is provided.

Uploaded by

Marwan Monajjed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

4/5/2021 Use Python to build a Dice Roller App | by John Kundycki | Towards Data Science

Get started Open in app

Follow 576K Followers

Use Python to build a Dice Roller App


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

John Kundycki Nov 12, 2020 · 4 min read

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.

Creating the function


For this part of the project, I imported 2 libraries: statistics and randint (from random).
The statistics library is 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.

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)

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.
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()

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…

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

Get started Open in app

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

Best of luck to you and thanks for reading!


Get started Open in app

Sign up for The Variable


By 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

Get this newsletter

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.

Python Data Science Projects Beginner Tutorial

About Help Legal

Get the Medium app

https://towardsdatascience.com/use-python-to-build-a-dice-roller-app-2408e66bf009 7/7

You might also like