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 Regular Expressions in Python Introduction to Regular Expressions Players Dictionary and Class

elmira bonab
elmira bonab
3,471 Points

I 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')
players.py
import re

string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''
Chris Grazioli
Chris Grazioli
31,225 Points

why are you using players_pattern= re.compile ? and in the Player Class why mystring?

2 Answers

The 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"

From 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
elmira bonab
3,471 Points

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