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

Charles Van Way
Charles Van Way
5,814 Points

Cannot get last challenge

Task 1 is simple enough: players = re.search(r''' (?P<name>(?P<last_name>[-\w ]*),\s?(?P<first_name>[-\w ]+):\s+)
(?P<score>[\d]+)?
''', string, re.X|re.M) But when I set up a Class called Players in the second step, I get an error, that the first task no longer works. And I can't tell of the Class is the correct answer to task 2.
Class Player: def_init_(self, last, first, score): self.last_name = last self.first_name = first self.score = score
I've obviously missed the point of the exercise, but I can't seem to get past this.

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<name>(?P<last_name>[-\w ]*),\s?(?P<first_name>[-\w ]+):\s+)   # Last and first names
    (?P<score>[\d]+)?                                                   #  Scores
''', string, re.X|re.M) 

Class Player:

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

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Charles, I don't think you've missed the point, but there are a few minor issues :)

  1. Remove the name capture group from the regular expression
  2. The class keyword must begin with a lowercase c
  3. The parameter names on the __init__ method must match those from the capture groups. The tests appear to be run using dictionary unpacking, this is why you cannot have the name group present, as all of the captured groups will be used when instantiating the Player objects. You can learn more about dictionary unpacking in the Python collections course https://teamtreehouse.com/library/packing-and-unpacking-dictionaries.
class Player:
    def __init__(self, last_name, first_name, score):
        # set properties
Charles Van Way
Charles Van Way
5,814 Points

Thank you, Umesh!! That made it all better. I do appreciate the time you took to give me your critique. Very well done!