Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Players Dictionary and Class

KARTIK BHASIN
KARTIK BHASIN
9,299 Points

Don't know what to do?

What does "wasn't able to create correct instances" means?

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(?P<first_name>[\w ]+):\s(?P<score>[\d]+)", string, re.M)
class Player:
    last_name = "last_name"
    first_name = "first_name"
    score = "score"
    string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''
    def __init__(self,last_name,score,first_name):
        self.players = re.search(r"(?P<{}>[\w ]+),\s(?P<{}>[\w ]+):\s(?P<{}>[\d]+)".format(self.last_name, self.first_name, self.score), self.string, re.M)

2 Answers

Steven Parker
Steven Parker
220,598 Points

:point_right: It looks like you're getting a bit too fancy there. You don't need to build the string or the re.search into the class. Just simply define the three properties mentioned and set them from the arguments passed to __init__.

Also, remember that score holds a number, not a string.

I'll bet you can get it now without an explicit spoiler.

Igor Protsenko
Igor Protsenko
15,651 Points

You can do it with def init(self,**kwargs): for key,value in kwargs.items(): setarrt(self,key,value)

Kenneth covered it in the previous courses