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 trialRobert Allen
10,550 PointsStuck 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?
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
Treehouse Moderator 68,457 PointsMy 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
Treehouse TeacherRobert:
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
29,379 PointsNotice that the final person in the list has a \s in his first and last names?