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 trialbaderalsayegh
19,026 Pointshavinf problem with the int varaible in re.find all
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 really dont understand why it cannot read my int variable as a multiplier of the string len,
the code is working if you change the word = re.findall(r'\w{a}', str) into word = re.findall(r'\w{6}', str) the number it self.
can you help me out of here thank you
import re
# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(a, str):
new = []
word = re.findall(r'\w{a,}', str)
new.append(word)
return new
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThe issue is the variable a
is not being used. In the regex string r'\w{a,}'
the "a" is treated as the Unicode character "a" and not interpreted as the variable a
.
To use the variable a
, it must be included outside of the string:
r'\w{' + str(a) + ',}'
or inserted through string formatting:
r'\w{{{0},}}'.format(a)
# or
r'\w{%s,}' % a
The second issue is the return value. findall()
returns a list
, so word
can be returned directly without appending it into the list new
return word
baderalsayegh
19,026 Pointsbaderalsayegh
19,026 Pointsthanks cris