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

Daniel Tompkins
Daniel Tompkins
5,591 Points

Can someone help me out with "players.py"? I'm not sure what more they're asking me for...

PROMPT: "OK, now, create a class named Player that has those same three attributes, last_name, first_name, and score. I should be able to set them through init."

players.py
import re

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

players = re.match(r'''
    ^(?P<last_name>[\w\s]+),\s
    (?P<first_name>[\w\s]+):\s
    (?P<score>[\d]+)$
''', string, re.M|re.X)

players = re.compile(r'''
    ^(?P<last_name>[\w\s]+),\s
    (?P<first_name>[\w\s]+):\s
    (?P<score>[\d]+)$
''', re.M|re.X)

class Player:
    def __init__():
        last_name = []
        first_name = []
        score = []
        for match in players.finditer(string):
            last_name.append('{}'.format(match.group('last_name')))
            first_name.append('{}'.format(match.group('first_name')))
            score.append('{}'.format(match.group('score')))

2 Answers

fahad lashari
fahad lashari
7,693 Points

They are asking for a class named player, which you have created.

Now create the three attributes like so:

class Player:
          last_name = ""
          first_name = ""
          score = 0

Now create the method that allows one to set the attributes like so:

        def __init__(self, **kwargs):
                  for key, value in kwargs.items():
                             setattr(self, key, value)

This is taught in the Python Object Oriented programming Track. Please refer to it for more information.

Hope that helps.

Kind regards,

Fahad

Daniel Tompkins
Daniel Tompkins
5,591 Points

Thank you very much! I'll return to that lesson. I forgot the setattr() method, and I'm still a little shaky on the key word argument unpacking.

Best,

Dan

fahad lashari
fahad lashari
7,693 Points

Hi Daniel,

Go through the recently updated collections track and also the object oriented track. You will find it in much detail there.

Kind regards,

Fahad

If this helped. Please mark it as best answer. Many thanks. And good luck.