Welcome to the Treehouse Community
Looking to learn something new?
Robert Allen10,550 Points
Stuck on Challenge Task 1
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)
Chris FreemanTreehouse Moderator 68,166 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.match() to capture three groups:
score. It should include
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!
Iain Diamond29,379 Points
Notice that the final person in the list has a \s in his first and last names?