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

Robert Allen
Robert Allen
10,550 Points

Stuck on Challenge Task 1

Hello,

I'm seeing this when I run my code:

Bummer! Didn't get the right results. Got [('Love', 'Kenneth', '20'), ('Chalkley', 'Andrew', '25'), ('McFarland', 'Dave', '10'), ('Kesten', 'Joy', '22')].

Any idea what I've done wrong?

players.py
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
    (?P<first_name>[\w]+):\s
    (?P<score>[\w]+)?
''', string, re.X|re.M)

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

My solution from this post:

In the Players Dictionary and Class Challenge Task 1 of 2

Create a variable named players that is an re.search() or re.match() to capture three groups: last_name, first_name, and score. It should include re.MULTILINE.

Your current code has a few issues:

players = re.search r'(?P<last_name>\w),\s(?Pfirst_name>\w+):\s(?P<score>\dt',string,re.M

Here are the issues:

  • re.search() is a method. It's arguments need to be wrapped in parens "( )".
  • In the sample string, names can contain spaces. Add a space to the matching character set
  • Last name needs to match more than a single character. Add plus sign to character set
  • Score needs to match more than a single digit. Add plus sign to character set. **For readability I also added brackets "[]"
  • <code><</code>first_name<code>></code> parameter is missing the leading "<code><</code>". Add missing "<code><</code>"
  • The last named pattern <code><</code>score<code>></code> is missing closing paren ")". Add closing paren.
  • There is an extra trailing "t" in the regular expression. Remove trailing "t"
players = re.search(r'(?P<last_name>[\w ]+),\s(?P<first_name>[\w ]+):\s(?P<score>[\d]+)', string, re.M)

The end result: Well done! You're doing great!

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Robert:

chris freeman does a great job explaining this challenge in this post.

Take a look and post back if you have additional questions.

Happy coding,

Ken

[MOD: fixed broken link -cf]

Iain Diamond
Iain Diamond
29,379 Points

Notice that the final person in the list has a \s in his first and last names?