diceroller.py

Created by matt-numworks

Created on December 12, 2024

1.08 KB


import random

def die_roll_simulation(num_rolls):
    roll_counts = [0] * 6  # Initialize counts for die faces 1 to 6
    for _ in range(num_rolls):
        roll = random.randint(1, 6)
        roll_counts[roll - 1] += 1  # Increment the count for the rolled face
    return roll_counts

while True:
    try:
        # Informing the user they can type 'exit' to stop
        print("Type 'exit' to exit script.")
        
        # User input for the number of rolls or an exit command
        user_input = input("Enter the # of die rolls: ").strip()
        
        if user_input.lower() == 'exit':
            print("Exiting the script. Goodbye!")
            break
        
        num_rolls = int(user_input)

        # Simulate die rolls
        roll_counts = die_roll_simulation(num_rolls)

        # Display the results in text
        print("\nResults after {} die rolls:".format(num_rolls))
        for i in range(6):
            print("Face {}: {}".format(i + 1, roll_counts[i]))

    except ValueError:
        print("Please enter a valid number or type 'exit' to stop.")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.