How To: “Roll Dice” with Python

Technology is always evolving and that means that you must evolve as a developer too. While coding boot camp teaches you about JavaScript and several of the frameworks you can use in JS, they also encourage you to seek out other languages/projects on your own time. It’s easy to pick up different languages once you know one very well, and I’ve chosen to do that with Python.

If you’re brand spankin’ new to Python or wondering whether Python is the right language for you, check out these guides on getting started with Python (Pro-tip: reading the documentation on a language/framework like in this link helps a lot) or choosing the right language. For this tutorial, all you’ll need is:

  • Python3 installed
  • A text editor (I recommend Atom)
  • Basic command line skills (navigating to a folder, creating a file)

During my journey learning Python, I’ve realized that there aren’t many step-by-step tutorials that guide you through writing your first program that explain what the code is doing, so, if you’re looking to write a simple Roll Dice program using Python, you’ve come to the right place.

Are you ready?

The first thing we’ll need to do is set up our program. Create a file called dice.py or something similar so you can easily identify it. Set up the file by starting out with:

import random
def main():

main()

random will allow us to randomly generate a number

We will be setting up our program “brains” in the def main(): function and will be calling that function with main().

Next, we will want to declare our variables, as well as think about what exactly we want our program to do. Our “Roll Dice” program will be taking user input to ask how many times they would like to roll the “6-sided” dice, then roll the dice that many times!

We’ll need two variables to set up the dice which are min and max which will serve as our “sides” for the dice. We need these two variables so that when we select a random number when the dice is rolled, the random function can choose any number between the min and max variables, including min and max. These will need to be declared within the main() function so any other functions can use them. Next, we’ll define a function that will roll the dice. Your code should look something like this:

import random
def main():
min = 1
max = 6
  def rollDice():
# empty for now
main()

In the rollDice() function, we’ll take the user input using input(), interpret it as an integer using int(), and use a count variable to keep track of how many rolls the program has run, roll the dice userinput amount of times, and display those rolls on the console.

I accomplished all of this by storing the user input in a variable like this:

numberOfRolls = int(input("How many times would you like to roll the dice? ")),

stored the count with:

count = numberOfRolls.

The logic

Before we code the logic, let’s talk about it.

We want our program to roll the dice numberOfRolls times. We need a way to count how many times the dice has been rolled so that our program knows when to stop. We do this using a while loop. We want our program to run while numberOfRolls is greater than 0 (because only have a limited number of rolls as specified by the user). We’ll use count to keep track of the rolls, and also keep track of when the program has rolled all of the rolls by decreasing the count. Additionally, we want our program to output a random number between 1 and 6 which simulates rolling a dice; we achieve this by using random.randint(min, max).

random is a module in Python that makes several functions available for use.

.randint(x, y) is a function available through random that takes two parameters (in this case, two variablesx and y that can be anything you set those parameters to), where it will select a random integer between x and y, including x and y.

Altogether, it looks like this:

while numberOfRolls >= count and count > 0:
print(random.randint(min, max))
count = count -1

Your program should look like this (don’t forget to call your rollDice() function):

import random
def main():
min = 1
max = 6
  def rollDice():
numberOfRolls = int(input("How many times would you like to roll the dice? "))
count = numberOfRolls
  while numberOfRolls >= count and count > 0:
print(random.randint(min, max))
count = count -1
  rollDice()
main()

There you have it! You’ve written your first “Roll Dice” program in Python! You can test it from your terminal (if you installed Python at the beginning of this tutorial) by navigating to the folder you created your program in and entering:

python3 dice.py

Many programmers frown upon giving the solution out to new programmers, and while I understand and agree with that, it’s hard to get to know a language without practicing it and being able to understand what exactly your code is doing and when. Here are some bonuses to try for yourself and get practice:

  • Ask the user if they’d like to roll again, and based on user input, roll again or end program
  • Only take in an integer greater than zero for numberOfRolls input and yell at the user if they enter anything other than a number
  • And anything else you want to make your program do!

You can follow my journey coding in Python through my Github.

Thanks for checking out my tutorial, and if you have any questions or comments, feel free to reach out!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.