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 trialLuis Garcia
3,468 PointsI'm not sure what I am being asked to do in task 2 of Code Challenge: Players Dictionary and Class.
This is my code, I'm not sure what I am being asked to do in Task 2. "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."
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.X|re.M)
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.X|re.M)
class Player():
def __init__(first_name1, last_name1, score1):
first_name = first_name1
last_name = last_name1
score = score1
[MOD: added ```python markdown formatting-cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYour code is on the right track. The argument names need to match the search group names, so replace "first_name1
" with "first_name
", etc.
Next, since __init__
is a method of Player
the first parameter should be self
to refer to the instance that is being initialized. Each of the assignments must also reference this newly created instance using self.score = score
, etc.
Post back if you need more help.
Luis Garcia
3,468 PointsHaha, I totally forgot the fundamentals there. Thanks a bunch.