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

Python: Regular Expressions Task - Create a class Player with three attributes

This is Challenge Task 2 of 2.

The question: 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.

I tried taking in the groups from players into the class Player and assigning them to the three attributes, last_name, first_name, and score.

import re

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

players = re.search("""
  (?P<first_name>[\w\s]+),\s   # first name
  (?P<last_name>[\w\s]+):\s   # last name
  (?P<score>\d\d)   # score
""", string, re.X|re.M)

class Player:
  def __init__(self, players):
    self.first_name = players.group("first_name")
    self.last_name = players.group("last_name")
    self.score = players.group("score")

I'm not sure what to do, am I heading in the right direction with this one?

Any help would be much appreciated.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're not getting all of the matches, or even a match object. You're getting keyword arguments that you need to handle in your __init__() function.