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

Are the last_name, first_name, and score attributes the ones of an Player object, or the class attributes?

I believe I tried both and they are failing. I'm lost.

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{1,9})$
''', string, re.X|re.M)

class Player:
  def __init__(self):
    d = players.groupdict()
    self.last_name = d['last_name']
    self.first_name = d['first_name']
    self.score = d['score']

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

The solution might be easier than you think.

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

Thanks. That works. However, I'm still stunned that this second code challenge has no relevance with regex using words like "the same last_name..." that seemingly suggest to connect with the first code challenge on a regex pattern. Why should there be two codes in a single file of "players.py"? I think if we make the challenges more educational, we had better provide an API to show how to use the regex in an object creation.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You are using the regex in object creation. And you're doing it in the right manner. Why would you create a class object directly from a string? That requires a huge amount of coordination between the different pieces of code. Better for the class to expect some attributes and not worry about where they come from.

Totally agreed as to a sound design of the Player class. And I'd vote for eliminating the second challenge out of the regex module as it has nothing to do with.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It has nothing to do with the regular expression engine, no. It does, however, have to do with the course (it's, effectively, what I told you to do as extra credit at the end of the last video) and it ties into how to make further use of the data you harvest from a regular expression. Most of the code you'll write in the real world will not be 100% pure and segregated code.

Thanks! Sometimes students are blind to see teacher's intent.