
Seth Gorham
5,016 PointsPossible error in 'test_die' unit test case?
In the downloaded unit test file 'test_dice.py' there is a test case 'test_die' which creates a default instance of the class 'Die' without any arguments:
class DiceTests(unittest.TestCase):
def test_die(self):
die = dice.Die()
self.assertIn(int(die), range(1, 2))
When no arguments are passed, the 'Die' class defaults to 2 sides (basically a coin) with a random value within the range of sides, so in this case a random value of 1 or 2:
class Die:
def __init__(self, sides=2, value=0):
if value > sides:
sides = value
if not sides >= 2:
raise ValueError("Must have at least 2 sides")
if not isinstance(sides, int):
raise ValueError("Sides must be a whole number")
if not value in range(0, sides + 1):
raise ValueError(f'Roll value must be between 1 and {sides}')
self.sides = sides
self.value = value or random.randint(1, sides)
Given the possible values of 1 or 2 for a default instance of 'Die', shouldn't the line
self.assertIn(int(die), range(1, 2))
in the 'test_die' case above be
self.assertIn(int(die), range(1, 3))
in order for the stop value of the range to include 2?
1 Answer

Steven Parker
204,855 PointsGood point! And for that matter, this line in "Die" stands out also:
if not value in range(0, sides + 1):
While this handles the upper limit correctly, the lower limit should probably be "1".
Anyway, you can report bugs as described on the Support page. You may even get an "Exterminator" badge.
Seth Gorham
5,016 PointsSeth Gorham
5,016 PointsHi Steven, Thanks for your reply. I will look into reporting this via the Support page. As for the line you mentioned:
if not value in range(0, sides + 1):
The 0 is there to catch the default value from the parameter definition. The error message could be written better to clarify that a value of zero means a random value (die roll) is desired.