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 Word Length

omri luz
omri luz
5,148 Points

whats wrong with my code

i dont know whats wrong with my code i even looked at the community and did the same but it still doesnt work

word_length.py
import re
# EXAMPLE: 
# >>> find_words(4, "dog, cat, baby, balloon, me") 
# ['baby', 'balloon'] 
def find_words(c, s):
    return re.findall(r'\w{c,}', s)

1 Answer

John Lack-Wilson
John Lack-Wilson
8,181 Points

The problem here is that the 'c' variable is being treated as the literal character 'c', rather than the integer that you want it to be treated as.

Below code should work for you

def find_words(c,s):
    regex = r'\w{'+str(c)+',}'
    return re.findall(regex,s)

So the variable regex is being assigned the regular expression to pass to the findall function. This allows us to treat the c variable as a number and convert it to a string with, the str() function, so that the count is passed correctly as a string.