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

Players Dictionary and Class part 2. Vague requirements.

https://teamtreehouse.com/library/regular-expressions-in-python/introduction-to-regular-expressions/players-dictionary-and-class

As part 2 of the question reads: "OK, now, create a class named Player that has those same three attributes <edit -- as the regex equation>, last_name, first_name, and score. I should be able to set them through init."

import re

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

regex code from part one:

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

Right. So make attributes within init to be set on object creation.

Attributes set to what? I assume the values of the three regex groups, as it would be redundant to set them to keys. and I assume that since re.match() and re.search would only come up with one set of groups then it is asking me to match each independently.

Set to be a choice between the three possible sets of groups? I assume not because I have the option of doing an re.match() or re.search(). So, just set the attributes to the possible values.

I tried all the ways I know how to write that. Then I tried getting the values from outside init so values could be assigned without the self., or setting the attributes to the keys instead of values.

'dictionary' in the title implies that it wants a dictionary somewhere in here, but I've heard no word of dictionary, key or value in the explanation so I steered clear of using one directly (although I tried through **kwargs in one of my many attempts at figuring out what it wants from me).

class Player(): 
          def init(self, players):
               self.last_name, self.first_name, self.score = players.groups() 
               # players.groups() returns tuple of ('Love', 'Kenneth', '20') 
               # my simplest solution out of about 10.

error message reads: init() got an unexpected keyword argument: 'last_name'.

TL;DNR: I'm overthinking this. If someone could help me understand the question, and help me understand why it should be obvious what it is asking of me, that would be awesome.

Austin Hawkins-Seagram, can you provide a link to the challenge that you are trying to complete? It may help provide more information.

BTW, your question is very extensive and inclusive. ;)

jacinator updated!

1 Answer

You are kind of over thinking this. The code that they want you to enter looks like this.

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

It has nothing to do with regular expressions really. They just want you to make a class.

God damn. I swore I tried that. Thank you.