Member-only story
Interview Questions: Write Yahtzee in Python
A game of random dice
Maybe you found this piece because you’re solving an assignment at school. Checking dice for results in Yahtzee is a popular task for Python classes. It’s also a really fun one because there are so many different things to check for.
In this article, I’ll guide you through most of the code you need to build a functional Yahtzee game. We’re going to write the whole thing in Python. The user interacts with the game on their keyboard in the terminal.
I started doing some research on Yahtzee and I found some interesting information. Apparently, it’s played differently throughout the world.
To my surprise, there were several students in my class who had never played Yahtzee. So here’s a brief introduction to the game.
The gameplay goes like this:
- Roll five dice.
- Keep the ones you want.
- Roll the ones you decided not to keep. This is your second roll.
- Keep the ones you want from your second roll and add them to the keep pile.
- Roll one last time. This is your third roll and you are forced to keep these dice.
- Now that you have your final stack of dice, you check if it’s valid. Then you add your total score, by counting all the dots on the dice, to that field. Say you roll a — “full house” (3 + 2) 6, 6, 6, 5, 5 — you then add 28 to the “Full House” field.
Here’s a more visual representation:

The Scoreboard
The scoreboard is split into two sections. The first section is about collecting aces(1), twos, threes, fours, fives, and sixes. If you roll three twos, you add the value six to the scoreboard on your twos section.
If your total in the first section is ≥ 63, you get an additional bonus of 50 points.
The lower section has more fun combinations:
- 1 pair
- 2 pairs
- 3 of a kind