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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![elmira bonab](https://secure.gravatar.com/avatar/7589c19f6a34b42ef5b8bdd89a9317cb?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
elmira bonab
3,471 PointsI am not sure how to include a regular expression in __init__ in a class
Here is my code:
string = '''Love, Kennetch: 20
Chalkey, Andrew: 25'''
players_pattern = re.compile(r'''
^(?P<name>(?P<last_name>[\w\s]+),\s
(?P<first_name>[\w\s]+)):\s
(?P<score>\d{2})$
''', re.X|re.M)
class Player():
def __init__(self,mystring):
self.first_name = players_pattern.search().group('first_name')
self.last_name = players_pattern.search().group('last_name')
self.score = players_pattern.search().group('score')
for match in players_pattern.finditer(mystring):
self.first_name = match.group('first_name')
self.last_name = match.group('last_name')
self.score = match.group('score')
import re
string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''
2 Answers
![Myers Carpenter](https://secure.gravatar.com/avatar/b6b3c3d23f4181308418df6c9586acba?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Myers Carpenter
6,421 PointsThe second task to this challenge doesn't have anything to do with regexes. It took me a few reads of the directions to before I understood that. It's asking for a class that you would create an instance like this
player1 = Player(first_name="Myers", last_name="Carpenter", score=9001)
print(player1.first_name) # -> "Myers"
![Myers Carpenter](https://secure.gravatar.com/avatar/b6b3c3d23f4181308418df6c9586acba?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Myers Carpenter
6,421 PointsFrom reading the description of the challenge I would think that your answer is going to look like:
players = re.search(SOME_REGEX_HERE)
![elmira bonab](https://secure.gravatar.com/avatar/7589c19f6a34b42ef5b8bdd89a9317cb?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
elmira bonab
3,471 Pointsthanks for the response! Your answer for the first part of challenge is correct, but my question is about the second part of the challenge which we need to create a class named Player that has those same three attributes, last_name, first_name, and score.
Chris Grazioli
31,225 PointsChris Grazioli
31,225 Pointswhy are you using players_pattern= re.compile ? and in the Player Class why mystring?