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

Dmitry Bruhanov
Dmitry Bruhanov
8,513 Points

players.py - regular expressions in Python

I checked the code in a separate console and found that after this code the respective fields of the class are initialized from the players' respective fields. Where did I go wrong? Why does the challenge say, that the init got an unexpected first_name or srore parameter. Thanks.

players.py
import re

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

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

class Player:
    def __init__(self, players):

        self.last_name = players.group('last_name')
        self.first_name = players.group('first_name')
        self.score = players.group('score')

3 Answers

Martin Nørskov Jensen
Martin Nørskov Jensen
17,612 Points

Hi again!

I can see i made an error in my code, the attributes names should be last_name and first_name like this:

class Player:
    def __init__(self, last_name="", first_name="", score=0):

        self.last_name = last_name
        self.first_name = first_name
        self.score = score

I have tested this one in the code challenge and it works

Martin Nørskov Jensen
Martin Nørskov Jensen
17,612 Points

Your player class is wrong it should take an unpacked dir

class Player:
    def __init__(self, lastname="", firstname="", score=0):

        self.last_name = lastname
        self.first_name = firstname
        self.score = score
Dmitry Bruhanov
Dmitry Bruhanov
8,513 Points

Martin, thanks for your suggestion. Itried it and here's what I got:

Bummer! init() got an unexpected keyword argument 'last_name'

Dmitry Bruhanov
Dmitry Bruhanov
8,513 Points

Thank you, Martin! This time it worked!!!