Skip to content

Commit 5daa11f

Browse files
committed
Invalid values now return none
1 parent 1cbc7d6 commit 5daa11f

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

dice_notation/dice.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ def __init__(self, quantity, sides):
127127
def roll(self):
128128
result = 0
129129

130-
if self.quantity and self.sides and self.sides > 0:
130+
if self.quantity == 0 or self.sides == 0:
131+
result = 0
132+
elif self.quantity > 0 and self.sides > 0:
131133
for x in xrange(self.quantity):
132134
result += randint(1, self.sides)
135+
else:
136+
result = None
133137

134138
return result

tests/test_dice.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,28 @@ def test_negativeQuantity(self):
110110
"""
111111
dice = RollableDice(-1, 1)
112112

113-
self.assertEqual(0, dice.roll())
113+
self.assertIsNone(dice.roll())
114114

115115
def test_negativeSides(self):
116116
"""
117117
Tests that quantity of dice is multiplied by the sides when rolling.
118118
"""
119119
dice = RollableDice(1, -1)
120120

121-
self.assertEqual(0, dice.roll())
121+
self.assertIsNone(dice.roll())
122122

123123
def test_noneQuantity(self):
124124
"""
125125
Tests that quantity of dice is multiplied by the sides when rolling.
126126
"""
127127
dice = RollableDice(None, 1)
128128

129-
self.assertEqual(0, dice.roll())
129+
self.assertIsNone(dice.roll())
130130

131131
def test_noneSides(self):
132132
"""
133133
Tests that quantity of dice is multiplied by the sides when rolling.
134134
"""
135135
dice = RollableDice(1, None)
136136

137-
self.assertEqual(0, dice.roll())
137+
self.assertIsNone(dice.roll())

0 commit comments

Comments
 (0)