Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba0fd4d

Browse filesBrowse files
committed
added multiplication modifier
1 parent 940d55a commit ba0fd4d
Copy full SHA for ba0fd4d

File tree

Expand file treeCollapse file tree

1 file changed

+18
-14
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-14
lines changed

‎diceRoller.py

Copy file name to clipboardExpand all lines: diceRoller.py
+18-14Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
66
Enter what kind and how many dice to roll. The format is the number of
77
dice, followed by the "d", followed by the number of sides the dice have.
8-
You can also add a plus or minus adjustment.
8+
You can also add a plus, minus, or multiplication adjustment.
99
1010
Ex:
1111
3d6 rolls three 6-sided dice
1212
1d10+2 rolls one 10-sided die, and adds 2
1313
2d38-1 rolls two 38-sided dice, and subtracts 1
14+
2d20*2 rolls two 20-sided dice, and multiplies the result by 2
1415
QUIT quits the program
1516
''')
1617

@@ -36,10 +37,8 @@
3637
raise Exception('Missing the number of dice.')
3738
numberOfDice = int(numberOfDice)
3839

39-
# Find if there is a plus or minus sign for a modifier:
40-
modIndex = diceStr.find('+')
41-
if modIndex == -1:
42-
modIndex = diceStr.find('-')
40+
# Find if there is a plus, minus, or multiplication sign for a modifier:
41+
modIndex = max(diceStr.find('+'), diceStr.find('-'), diceStr.find('*'))
4342

4443
# Find the number of sides. (The "6" in "3d6+1"):
4544
if modIndex == -1:
@@ -53,30 +52,35 @@
5352
# Find the modifier amount. (The "1" in "3d6+1"):
5453
if modIndex == -1:
5554
modAmount = 0
55+
modOperator = None
5656
else:
5757
modAmount = int(diceStr[modIndex + 1:])
58-
if diceStr[modIndex] == '-':
59-
# Change the modification amount to negative
60-
modAmount = -modAmount
58+
modOperator = diceStr[modIndex]
6159

6260
# Simulate the dice rolls:
6361
rolls = []
6462
for i in range(numberOfDice):
6563
rollResult = random.randint(1, numberOfSides)
6664
rolls.append(rollResult)
6765

66+
# Calculate the total:
67+
total = sum(rolls)
68+
if modOperator == '+':
69+
total += modAmount
70+
elif modOperator == '-':
71+
total -= modAmount
72+
elif modOperator == '*':
73+
total *= modAmount
74+
6875
# Display the total:
69-
print('Total:', sum(rolls) + modAmount, '(Each die:', end='')
76+
print('Total:', total, '(Each die:', end=' ')
7077

7178
# Display the individual rolls:
72-
for i, roll in enumerate(rolls):
73-
rolls[i] = str(roll)
74-
print(', '.join(rolls), end='')
79+
print(', '.join(map(str, rolls)), end='')
7580

7681
# Display the modifier amount:
7782
if modAmount != 0:
78-
modSign = diceStr[modIndex]
79-
print(', {}{}'.format(modSign, abs(modAmount)), end='')
83+
print(', {}{}'.format(modOperator, abs(modAmount)), end='')
8084
print(')')
8185

8286
except Exception as exc:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.