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 Python Testing Be Assertive Membership and Other Assertions

Anthony Grodowski
Anthony Grodowski
4,902 Points

.group()

Could someone please explain me what .grup() function does? I've seen it in the dice.py code but have no idea what it does since I think it hasn't been covered in any of the Python course so far. Thanks!

1 Answer

Hopefully, I understand your question right. I think you are referring to this line of code in dice.py

num, sides = die_pattern.match(description).groups()

This is associated with Python Regular Expression. I am providing this link here for you. Remember how Kenneth Love mentions that you can make a roll of dice with a string, e.g. '1d6'. If this description string you provide matches with the die_pattern

die_pattern = re.compile(r'^(?P<num>\d+)d(?P<sides>\d+)$', re.I)

then the function groups() will return a tuple containing all the subgroups which mean the matched text. That is why you would see that these two matched texts are being assigned to num and sides

num, sides = die_pattern.match(description).groups()

Hope that this helps you!