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

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
229,732 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