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

hugo dahl
hugo dahl
2,750 Points

drhd

hrlp

players.py
import re

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

# Example usage:
player_string = "Doe, John: 100"

# Extracting values using regex
attrs = re.search(r'^(?P<last_name>[\w\s]+),\s(?P<first_name>[\w\s]+):\s(?P<score>[\d]+)$', player_string)
if attrs:
    attrs = attrs.groupdict()
    player = Player(attrs['last_name'], attrs['first_name'], attrs['score'])
    print(player.last_name)  # Output: Doe
    print(player.first_name)  # Output: John
    print(player.score)  # Output: 100
else:
    print("Invalid input format for Player")

1 Answer

Steven Parker
Steven Parker
229,744 Points

It looks like you accidentally pasted some unrelated code over the challenge, removing the provided starter code in the process.

You'll need to start over to get the original provided code back and then add to it following the instructions. Don't remove the starter code with the data "string".