How To Make A Simple Dice Game In Python

mbvissers.eth
Geek Culture
Published in
3 min readMay 31, 2021

A simple way to learn basic Python.

Photo by Ian Gonzalez on Unsplash

This tutorial is a very simple and very well-known case to help with learning how to program applications. It tackles every basic concept from variables to loops and even importing libraries for the random function.

Note: I will assume that you have Python 3 installed and that you have at least a very basic sense of programming already. I will also assume that you know how to run a Python file.

The Game

Before we can program the little dice game, we need to know how it works and what we need to build. We will take the initial idea and separate it into small pieces that we can easily program.

The basic idea of the game is that you roll one die and count up your score. If the die didn’t hit 1, you can try to roll again and add the new points to your total if you want, or you can choose to keep the points you have so far. If you hit 1 however, all your points will be lost and your game will be over.

So to add this together into a small list of components, we will roughly get something like this:

  • Roll die
  • Check if die rolled 1
  • If it didn’t, add the points to our total
  • Ask if the user wants to roll again (and start at step 1)
  • If it did hit 1, end the game

This is a very small list, and it should be very easy to program as well. Even though it reaches most basic programming topics.

The Code

Now that we know what to program, let's try to do it fairly well so that we can easily adjust certain settings and build more upon our code later on. Create a new Python file and open your favorite editor.

Importing the random module

A chance-based game needs some randomness. This can easily be achieved by the randint function from the random module that ships with Python. You can import it at the top of your file.

The variables

I feel like the first thing we need to do is to set some variables. We can add a variable for our die, our number on which the user receives a game over, and any others we might need.

The game loop

Our game will run in a loop that will run whenever our run variable is True . It will continue to roll the die if the user wants to, and it will stop when the user wants to stop.

The roll

We need a function (or at least some code in the loop) that will roll the die, add points, and if we roll our trigger, will terminate the loop. This function will have a few nested if statements in which we need to check if the number is the same as our trigger, or if the user wants to roll again by checking an input we can make.

The final code

With these pieces together, we can show our final code. The code may look like a lot at first, but I’ve tried to add comments for everything I’ve done so you can hopefully learn from it if you haven’t been able to do it yourself.

The game in action

Conclusion

This very simple game is just another way to try and understand computer science and programming. Like the Hello World examples, a simple dice game is one of the many ways to start your journey, and I hope you’ve learned a bit from this one as well.

Have a wonderful day and thank you for reading.

--

--

mbvissers.eth
Geek Culture

I occasionally write about programming. Follow me on Twitter @0xmbvissers