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.
I got through the first stage of the challenge. But in the second stage, i was required to set the attributes into a class using the init method. I keep getting error messages. Please help. Thank you.
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 (?P<first_name>[\w ]+):\s (?P<score>\d+)$ ''', re.X|re.M) class Player: def __init__(self, last_name, first_name, score): for match in players.finditer(string): self.last_name = match.groupdict('last_name') self.first_name = match.groupdict('first_name') self.score = match.groupdict('score')
The challenge says: "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." You're trying to create players inside the
__init__ is a way to define how to create an object of the class Player. It's just a blueprint. You don't create specific objects inside it. Here's an example:
class Animal: def __init__(self, name, legs, sound): self.name = name self.legs = legs self.sound = sound # create one cat = Animal("cat", 4, "meow")
Try writing init using the example.
By the way, not sure how you passed the first part of the challenge, because the code you have doesn't work. Maybe you left out something when writing the question? The challenge says you have to use re.search or re.match. And you have to pass in
string to the function.
players = re.search(r''' ^(?P<last_name>[\w ]+),\s (?P<first_name>[\w ]+):\s (?P<score>\d+)$ ''', string, re.X|re.M)
@ursaminor Thank you so much for the insight. Actually I had used exactly your suggestion before but i got an error message and now I'm thinking it was probably due to a syntax problem but I wasn't very settled to find out further and then I started to try out other means and I later stumbled. And for the first part of the challenged, I changed the code in stage 2 just to fit the style I was trying to achieve with the class. So I got through with the challenge and I really appreciate your assistance on this. Thank you.