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

Not sure I understand the error here. Gives me: Bummer! __init__() got an unexpected keyword argument 'first_name'

I think it would be helpful to know how the object is being created.

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]+),\s
  (?P<first_name>[\w\s]+):\s
  (?P<score>\d+)$
  ''', string, re.X|re.MULTILINE)

class Player(object):
  def __init__(self, first, last, score):
    self.last_name = last
    self.first_name = first
    self.score = sc

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The parameters of the __init__ need to match the group names from the players search

class Player(object):
  def __init__(self, first_name, last_name, score):  #<-- fixed parameter names
    self.last_name = last_name
    self.first_name = first_name
    self.score = score  # < - fixed typo

So the names of my parameters actually matters? I thought I could name them anything so long as I assigned them to the correctly named attributes.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Ideally, you can name the parameters anything you like, but since the order of the __init__ parameters were not specified, the method will be called using keyword arguments (kwargs).

In this challenge, the grader is using the output of players to create Player instances and calling __init__ with keywords based on the group names from the re.search output. So the parameters much match the group names.

James N
James N
17,864 Points

thanks! this helped me, too!