You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
daily_programmer/python/dice_roller.py

52 lines
1.1 KiB

from random import randint
def roll(number, sides):
"""
roll a number of virtual dice with a given number of sides
"""
res = 0
throws = []
for die in range(number):
throws.append(randint(1, sides))
res += throws[-1]
print(res, ":", *throws)
return res
def parselist(path):
"""
parse a list containing dice rolls (one per line) a la
5d12
6d4
1d2
1d8
3d6
4d20
100d100
"""
roll_call = []
with open(path, 'r') as rolls:
for line in rolls:
line = line.strip()
roll_call.append(line.split('d'))
return roll_call
if __name__ == '__main__':
pali = parselist('dice.rolls')
print(pali)
for to_roll in pali:
n, s = to_roll
print("Rolling", n + "d" + s)
print("Result:", roll(int(n), int(s)), '\n')
# interactive mode
x = 0
while x != 'x':
x = input("please input what you want to throw as 'NdM', or 'x' to quit\n")
if len(x.split('d')) == 2:
n, m = x.split('d')
roll(int(n), int(m))