Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller Yatzy Scoring

Seth Gorham
Seth Gorham
5,016 Points

Possible 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
Steven Parker
229,708 Points

Good 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. :beetle:

Seth Gorham
Seth Gorham
5,016 Points

Hi 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.