Hello,
Goal: Create a simple program that recreates a table-top game mechanic; rolling dice with a varying number of sides.
Python: Using 3.5.2
Problem: The code pasted below seems to work fine, on the surface. However, if I run it using an input of "2d2", it will only ever produce a result of 3 or 4. I'm worried I'm missing something that is preventing the random generator from using the lowest number (1).
Code:
Goal: Create a simple program that recreates a table-top game mechanic; rolling dice with a varying number of sides.
Python: Using 3.5.2
Problem: The code pasted below seems to work fine, on the surface. However, if I run it using an input of "2d2", it will only ever produce a result of 3 or 4. I'm worried I'm missing something that is preventing the random generator from using the lowest number (1).
Code:
# Allow a user to input a dice in the classic '2d6' format
# (which would indicate 2 dice with 6 sides) and create the right random dice roll
import random
while True:
roll = input("What do you want to roll?")
if roll == "exit":
break
else:
# split the string at d. So '2d6' becomes ['2', '6']
splitRoll = roll.split("d")
# convert the string to an integer. so ['2', '6'] becomes [2,6]
intRoll = [int(i) for i in splitRoll]
# set (or reset) result to 0
result = 0
# create random numbers for each dice rolled and add them together
for i in range(intRoll[0]):
result += random.randint(1, intRoll[1])
print (result)
result = 0
