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 trialDavid Wright
4,437 Pointsword_length.py Please help. I think I'm close
Hello! Any advice on this would help. I'm getting the words in the list that are less than 6 still and I'm not sure why. Is this my only issue?
import re
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(count, string):
words_list = []
for itb in string:
words = re.findall(r'\D*', string)
for word in words:
if len(word) >= count:
return word
3 Answers
gaetano amoroso
2,993 PointsI just saw that I didn't answer exsatly at your question, so now I used findall for your solution
import re
def find_words(count, string):
l = re.findall(r"\b\w{%s,}\b" % count, string)
new_l = []
for word in l:
if len(word[:len(word)]) >= count:
new_l.append(word)
return new_l
print(find_words(4, "dog, cat, baby, balloon, mes"))
the %s, stands for {from count, to infinite numbers}, it is the same sintax for print function when it have to farmatting a string
best regards
jcorum
71,830 PointsSometimes the instructions are a bit difficult to interpret. What they want is a function more like this:
def find_words(count, data):
return re.findall(r"\b\w{%s,}\b" % count, data)
David Wright
4,437 PointsOkay. So what exactly does the {%s,} mean?
gaetano amoroso
2,993 Pointsgreat job, you are almost.
def find_words(count, string):
l=[]
word_list=string.split(",")
for word in word_list:
if len(word) >= count:
l.append(word)
return l
print(find_words(4, "dog,cat,baby,balloon,me"))
Your problem was that the followingcode retrive a list with two element, the firsth is a fullstring and the second is a empty string. I believe that you want one element in list for each word in string between the commas
words = re.findall(r'\D*', string)
I hope I was helpful
David Wright
4,437 PointsThis was helpful. Do you know a way to do it using the: re.findall() function? I was hoping to practice this.
David Wright
4,437 PointsAlso.. Is there a way to find out my output? As you did above with the print statement. I hate how I never get to see the output. I find that seeing how the output changes (even if I'm wrong) helps with the learning process.