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

Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

Help needed ! Regular expressions - Task

My code works fine in my console. But I could not get through the challenge. Please help

players.py
import re

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

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

3 Answers

Hanley Chan
Hanley Chan
27,771 Points

This worked for me:

class Player:
    def __init__(self, last_name, first_name, score):
        self.last_name = last_name
        self.first_name = first_name
        self.score = score
Keerthi Suria Kumar Arumugam
Keerthi Suria Kumar Arumugam
4,585 Points

Yeah. I figured that out too. The task objective was not clear. Anyways, thank you

Frederick Pearce
Frederick Pearce
10,677 Points

I also got Hanley's answer to work. There isn't a way to set each name in an instance without passing the values of each field into def init() is there? Wasn't that the basic problem?

import re

class Player:

    def __init__(self, last_name, first_name, score):
        self.last_name = last_name
        self.first_name = first_name
        self.score = score


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

line = re.compile(r"""
    ^(?P<last_name>[-\w ]*),\s
    (?P<first_name>[-\w ]*):\s
    (?P<score>[\d]*)$
""", re.X|re.M)

players = line.search(string)
Hanley Chan
Hanley Chan
27,771 Points

From what I understand about the question, it's asking to be able to set these attributes through the init function which is called automatically when an instance of the Player object is created.