Dice Roller Python Code

You can run this code and each time it executes, it will simulate rolling a dice and display the corresponding value on the screen.

In this version of code, there are three main functions:

  1. The roll_dice function allows you to specify the number of dice and the number of sides on each die. It returns a list of rolled dice values.
  2. The print function prints the rolled dice values in a user-friendly format.
  3. Ths unction calculates the sum of the rolled dice values.

The role with trick function introduces additional tricks for rolling the dice:

  • If you add ‘!’ to the number of dice, it will roll the specified number of dice and display their sum.
  • If you add ‘d’ to the number of dice, it will roll the specified number of dice with the specified number of sides on each die.
  • If you enter just a number, it will roll that number of 6-sided dice.

Code

import random

def roll_dice(num_dice=1, num_sides=6):
    """
    Simulates rolling multiple dice with a specified number of sides.

    Parameters:
    - num_dice (int): Number of dice to roll (default: 1)
    - num_sides (int): Number of sides on each die (default: 6)

    Returns:
    - List[int]: List of rolled dice values
    """
    dice_rolls = []
    for _ in range(num_dice):
        dice_value = random.randint(1, num_sides)
        dice_rolls.append(dice_value)
    return dice_rolls

def print_dice(dice_rolls):
    """
    Prints the rolled dice values.

    Parameters:
    - dice_rolls (List[int]): List of rolled dice values
    """
    for i, dice_value in enumerate(dice_rolls):
        print(f"Die {i + 1}: {dice_value}")

def sum_dice(dice_rolls):
    """
    Calculates the sum of the rolled dice values.

    Parameters:
    - dice_rolls (List[int]): List of rolled dice values

    Returns:
    - int: Sum of the rolled dice values
    """
    return sum(dice_rolls)

def roll_with_tricks():
    """
    Simulates rolling dice with additional tricks.

    Tricks:
    - Add '!' to the number of dice to roll to get the sum of all dice.
    - Add 'd' to the number of dice to roll to specify the number of sides on each die.

    Examples:
    - '2' will roll two 6-sided dice.
    - '3d10' will roll three 10-sided dice.
    - '4!' will roll four 6-sided dice and display the sum.

    Returns:
    - List[int] or int: List of rolled dice values or sum of the rolled dice values
    """
    trick_input = input("Enter the number of dice to roll (or use tricks): ").strip().lower()

    if trick_input.endswith('!'):
        num_dice = int(trick_input[:-1])
        dice_rolls = roll_dice(num_dice)
        print_dice(dice_rolls)
        return sum_dice(dice_rolls)
    
    if 'd' in trick_input:
        num_dice, num_sides = map(int, trick_input.split('d'))
        dice_rolls = roll_dice(num_dice, num_sides)
        print_dice(dice_rolls)
        return dice_rolls

    num_dice = int(trick_input)
    dice_rolls = roll_dice(num_dice)
    print_dice(dice_rolls)

# Roll the dice with tricks
roll_with_tricks()

Output

Note:- You can run this code and experiment with different inputs to roll dice, display the values, and calculate sums.