Python code for rolling two dice

Let’s begin by writing a simple code that gets output from two rolled dice:

import random

def roll_dice():
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    return dice1 + dice2

result = roll_dice()
print("Result:", result)

Following is a line-by-line explanation of the code:

  1. import random: This line imports the random module, which provides a suite of functions for generating pseudo-random numbers.
  2. def roll_dice():: This line defines a new function called roll_dice. This function generates and returns the sum of two random numbers, simulating the rolling of two dice.
  3. dice1 = random.randint(1, 6): This line generates a random integer between 1 and 6 (inclusive) and assigns it to the variable dice1. This represents the first die roll.
  4. dice2 = random.randint(1, 6): This line generates a random integer between 1 and 6 (inclusive) and assigns it to the variable dice2. This represents the second die roll.
  5. return dice1 + dice2: This line returns the sum of dice1 and dice2, which represents the total value of the two dice rolls.
  6. result = roll_dice(): This line calls the roll_dice function and assigns the returned value to the result variable.
  7. print("Result:", result): This line prints the result variable to the console, with the label “Result:”. This is the final output of the code, showing the sum of the two random dice rolls.

Following is the unit testing code

import unittest
import random

class TestRollDice(unittest.TestCase):
    def test_roll_dice(self):
        # Test that the sum of the rolls is in the expected range
        for i in range(100):
            result = roll_dice()
            self.assertTrue(2 <= result <= 12)

        # Test a few specific rolls
        self.assertEqual(roll_dice(), 2)
        self.assertEqual(roll_dice(), 12)

def roll_dice():
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    return dice1 + dice2

if __name__ == '__main__':
    unittest.main()

This code defines a test class TestRollDice that inherits from unittest.TestCase. This class contains two test methods, test_roll_dice, which verifies that the roll_dice function generates random numbers within the expected range (2 to 12) and that the function returns specific values when called.

The tests are run by calling unittest.main() at the end of the code. If all tests pass, you will see no errors.

Following is the code for a graphical interface for our code:

import tkinter as tk
import random

def roll_dice():
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    result_label.config(text=str(dice1 + dice2))

root = tk.Tk()
root.title("Two Dice Roller")

result_label = tk.Label(root, text="")
result_label.pack()

roll_button = tk.Button(root, text="Roll Dice", command=roll_dice)
roll_button.pack()

root.mainloop()

This code creates a window using the tkinter library and sets its title to “Two Dice Roller”. The window contains a label to display the result of the dice roll and a button to initiate the roll. The roll_dice function generates two random numbers, simulates the roll of two dice, and updates the label with the result. The mainloop method is called at the end to start the graphical interface event loop. When you run this code, you should see a window with a button labeled “Roll Dice”. When you press the button, two random numbers will be generated, simulating the roll of two dice, and the label will display the sum of the two numbers.