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

players.py

I got through the first stage of the challenge. But in the second stage, i was required to set the attributes into a class using the init method. I keep getting error messages. Please help. Thank you.

players.py
import re

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

players = re.compile(r'''
    ^(?P<last_name>[\w ]+),\s
    (?P<first_name>[\w ]+):\s
    (?P<score>\d+)$
''', re.X|re.M)

class Player:
    def __init__(self, last_name, first_name, score):
        for match in players.finditer(string):
            self.last_name = match.groupdict('last_name')
            self.first_name = match.groupdict('first_name')
            self.score = match.groupdict('score')     

2 Answers

The challenge says: "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." You're trying to create players inside the __init__. However, __init__ is a way to define how to create an object of the class Player. It's just a blueprint. You don't create specific objects inside it. Here's an example:

class Animal:
    def __init__(self, name, legs, sound):
        self.name = name
        self.legs = legs
        self.sound = sound

# create one
cat = Animal("cat", 4, "meow")

Try writing init using the example.

By the way, not sure how you passed the first part of the challenge, because the code you have doesn't work. Maybe you left out something when writing the question? The challenge says you have to use re.search or re.match. And you have to pass in string to the function.

players = re.search(r'''
    ^(?P<last_name>[\w ]+),\s
    (?P<first_name>[\w ]+):\s
    (?P<score>\d+)$
''', string, re.X|re.M)

@ursaminor Thank you so much for the insight. Actually I had used exactly your suggestion before but i got an error message and now I'm thinking it was probably due to a syntax problem but I wasn't very settled to find out further and then I started to try out other means and I later stumbled. And for the first part of the challenged, I changed the code in stage 2 just to fit the style I was trying to achieve with the class. So I got through with the challenge and I really appreciate your assistance on this. Thank you.