-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDic_Roller.py
More file actions
59 lines (56 loc) · 2.14 KB
/
Copy pathDic_Roller.py
File metadata and controls
59 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import random, sys
print('''
Dice Roller, by sinister geeek
Enter what kind and how many dice to roll. The format is the number of dice, followed by "d",followed by the number of sides the dice have. You can also add a plus or minus adjustment.
Examples:
3d6 rolls three 6-sided dice
1d10+2 rolls one 10-sided die, and adds 2
2d38-1 rolls two 380sided die, and subracts 1
QUIT quits the program
''')
while True:
try:
diceStr = input('> ')
if diceStr.upper() == 'QUIT':
print('Thanks for playing!')
sys.exit()
diceStr = diceStr.lower().replace(' ','')
dIndex =diceStr.find('d')
if dIndex == -1:
raise Exception('Missing the "d" character.')
numberOfDice = diceStr[:dIndex]
if not numberOfDice.isdecimal():
raise Exception('Missing the number of dice.')
numberOfDice = int(numberOfDice)
modIndex = diceStr.find('+')
if modIndex == -1:
modIndex = diceStr.find('-')
if modIndex == -1:
numberOfSides = diceStr[dIndex + 1 :]
else:
numberOfSides = diceStr[dIndex + 1 : modIndex]
if not numberOfSides.isdecimal():
raise Exception('Missing the number of sides.')
nuberOfSides = int(numberOfSides)
if modIndex == -1:
modAmount = 0
else:
modAmount = int(diceStr[modIndex + 1:])
if diceStr[modIndex] == '-':
modAmount = -modAmount
rolls = []
for i in range(numberOfdice):
rollResult = random.randint(1,numberOfSides)
rolls.append(rollResult)
print('Total',sum(rolls) + modAmount,'(Each die:',end='')
for i, roll in enumerate(rolls):
roll[i] = str(roll)
print(', '.join(rolls),end='')
if modAmount != 0:
modSign = diceStr[modIndex]
print(',{}{}'.format(modSign,abs(modAmount)),end='')
print(')')
except Exception as exc:
print('Invalid input.Enter something like "3d6" or "1d10+2".')
print('Input was invalid because: ' + str(exc))
continue