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 trialTobias Edwards
14,458 PointsIntroduction to Regular Expressions Final Challenge (classes)
Question: Wow! OK, now, 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.
Does this mean just grab the data from players and replicate this under init? I have attached my code so far
Any help on what to do and how to do it would be greatly appreciated.
Also, Merry Christmas!
import re
string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''
players = re.match('''
^(?P<last_name>[\w\s]+),\s
(?P<first_name>[\w\s]+):\s
(?P<score>\d\d)$
''', string, re.M|re.X)
class Player:
def __init__(self):
# last_name = fetch the data from players.last_name
# first_name = fetch the data from players.first_name
# score = fetch the data from players.score
5 Answers
Scott Dunstan
12,517 PointsHey Tobias, nope, it's simpler. You just need to set the variables - seems unrelated:
class Player:
def __init__(self, last_name, first_name, score):
self.last_name = last_name
self.first_name = first_name
self.score = score
Scott Dunstan
12,517 PointsGuilty of the same thing mate, results in a face-palm most times.
Oziel Perez
61,321 PointsActually this did not work for me at all! (well to be honest I didn't name my parameters "first_name", "last_name", "score", I named them "one", "two", "three"). It kept telling me that an unexpected keyword argument "<group_name>" was being passed in. I was stuck for hours on this until I finally noticed that when I rechecked my work without any changes, I noticed that the <group_name> changed sometimes. Sometimes it would be "first_name", sometimes "last_name", and so on. That led me to believe that it was several tuples or dictionaries being passed in, each argument having key and value pairs (ex: ("first_name", "Kenneth"), ("last_name", "Love")). Idk I was willing to try anything by that point, so this is what I typed out:
class Player:
def __init__(self, **kwargs):
for key, value in kwargs.items():
if key == "first_name":
self.first_name = value
elif key == "last_name":
self.last_name = value
elif key == "score":
self.score = value
it totally worked for me, and it makes sense since the match() object will only yield a tuple with all the values or a dicitonary if .groupdict() was used on the object. Hope this helps anyone else in case they get stuck as well and the above answers don't work
Denis Chernenko
4,077 Pointsthank you for good idea, which is more flexible in terms of working with data the only remark is == instead of = in "if" clause in your code
Oziel Perez
61,321 PointsDenis Chernenko, updated answer to your comment
John Peck
21,270 PointsThese challenges are terribly paced. Instructors, look up "pedagogy and scaffolding" please. This is a nightmare.
Seph Cordovano
17,400 PointsI've been trying to answer this question for 3hrs. This is ridiculously worded and unclear.
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 # Last name, comma, space
(?P<first_name>[\w ]+):\s # First name, colon, space
(?P<score>[\d]+)$ # Score
''', re.X | re.M)
class Player:
def __init__(self, last_name, first_name, score):
self.last_name = last_name
self.first_name = first_name
self.score = score
for player in players.finditer(string):
p = player.groupdict()
Player(p['last_name'], p['first_name'], p['score'])
Tobias Edwards
14,458 PointsTobias Edwards
14,458 PointsThanks, mate - I clearly over complicated things
David Vote
3,092 PointsDavid Vote
3,092 PointsYes, thanks for this, I was making it way more complicated than it needed to be. Cheers!