Python Forum

Full Version: Dice Rolling Simulator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import random

def roll_dice():
  """Rolls two six-sided dice and returns the sum."""
  die1 = random.randint(1, 6)
  die2 = random.randint(1, 6)
  return die1 + die2

def main():
  while True:
    roll = input("Would you like to roll the dice? (yes/no): ")
    if roll.lower() == "no":
      break
    
    sum = roll_dice()
    print("You rolled:", sum)

if __name__ == "__main__":
  main()
Can I add features like keeping track of the user's score or allowing them to choose the number of dice to roll?
Yes. Consider using a list for the dice values, so the number of dice is not predetermined.