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 trialKristine Saryan
1,386 PointsNot sure what I'm doing wrong
The instructions: Create a function named find_words that takes a count and a string. Return a list of all of the words in the string that are count word characters long or longer.
I know num is becoming part of the string, but I'm not sure how else to do this.
import re
def find_words (num, my_string):
my_list= []
my_list.append(re.findall(r'\w{num,}', my_string))
return my_list
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThere are two issues.
num
Needs to be formatted into the regex. Currently it's just the characters "num"re.findall()
returns a list so it doesn't need to be appended into another list. Simply assign the results tomy_list
Michael Kurfis
6,584 PointsThanks, Chris!
Michael Kurfis
6,584 PointsMichael Kurfis
6,584 PointsShouldn't
word_list = (re.findall(r'\w{{},}'.format(count), string))
work?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsMichael,
format
can be confused by the regex braces vs the format placeholders. The regex braces need to escaped by doubling them:r'\w{{{},}}'.format(count)
which can become hard to read.