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

Mike Brooks
Mike Brooks
3,389 Points

Do not understand the question.

In the final challenge of Python Regular expressions, the challenge says: "Wow! 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." Since we were doing regular expressions, I created a init(self, inTxt) method and used a regular expression to translate the inTxt into the three variables. When I tested in pyCharm it worked. When in ran it in the challenge window; it told me there was an unexpected "score" parameter????

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 ]*)
      [, ]{2}
      (?P<first_name>[-\w ]*)
      [: ]{2}
      (?P<score>\d{1,3})$
''', string, re.X | re.M)

class Player():
    last_name = ""
    first_name = ""
    score = ""

    def __init__(self, inTxt):
        xxx = re.match(r'''
                   (?P<last_name>^[-\w ]*)
                   [, ]{2}
                   (?P<first_name>[-\w ]*)
                   [: ]{2}
                   (?P<score>\d{1,3})$
''', inTxt, re.X | re.M)
        self.last_name = xxx.group("last_name")
        self.first_name = xxx.group("first_name")
        self.score = xxx.group("score")
Francesco Cabiddu
Francesco Cabiddu
2,297 Points

Hi mike! I had the same problem. I think that the solution is easier, because the challenge ask you to create a class that allows someone to add some specific arguments to it. In this sense if I define a general class like this:

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

than you will be able to define its arguments doing for example:

player = Player(**players.groupdict())

1 Answer

Mike Brooks
Mike Brooks
3,389 Points

Thanks very much. It will be the best answer as soon as I can figure out how to make it so.

Mike Brooks
Mike Brooks
3,389 Points

See previous comment by Francesco Cabiddu. It is the best answer.