Search code examples
python-3.xsimulatordice

Dice Rolling Simulator


I am making a Dice rolling simulator in python to play dnd. I am very new to python so please don't make fun of me if my code is really bad.

import random
while 1 == 1:
    dice = input("What kind of dice would you like to roll?: ") # This is asking what kind of dice to roll (d20, d12, d10, etc.)
    number = int(input("How many times?: ")) # This is the number of times that the program will roll the dice
    if dice == 'd20':
        print(random.randint(1,21) * number)
    elif dice == 'd12':
        print(random.randint(1,13) * number)
    elif dice == 'd10':
        print(random.randint(1,11) * number)
    elif dice == 'd8':
        print(random.randint(1,9) * number)
    elif dice == 'd6':
        print(random.randint(1,7) * number)
    elif dice == 'd4':
        print(random.randint(1,5) * number)
    elif dice == 'd100':
        print(random.randint(1,101) * number)
    elif dice == 'help':
        print("d100, d20, d12, d10, d8, d6, d4, help, quit")
    elif dice == 'quit':
        break
    else:
        print("That's not an option. Type help to get a list of different commands.")

quit()

My original attention was just to let it be and not make a number variable, but then my brother reminded me that some weapons have multiple rolls and instead of just rolling more than once, I want to have an input asking how many times to roll the dice. The problem with my code right now is that it will randomize the number and then times it by two. What I want it to do is times the number of different integers and add them together.


Solution

  • Maybe use a for-loop and iterate over the number of times the user wants to roll the dice, while saving those to a list to display each roll of the die.

    For example, the first die may look like this:

    rolls = []
    if dice == 'd20':
        for roll in range(number):
            rolls.append(random.randint(1,21))
        print('rolls: ', ', '.join([str(roll) for roll in rolls]))
        print('total:', sum(rolls))
    

    Example output:

    What kind of dice would you like to roll?: d20
    How many times?: 2
    rolls:  10, 15
    total: 25