The format for this function is numLen(s,n): where s is a string and n is an integer. What the code is supposed to do is return the number of words in the string that have length n, so:
numLen(“This is a test”, 4)
Would return 2, since two words have 4 characters.
def numLen(s, n):
'''
takes string s and integer n as parameters and returns the number of words
in the string s that have length n
'''
return s.split()
if len(s) == n:
return 'hello'
I attempted to split the string into a list and check the length of each word in that list, but that didn’t seem to work out. The farthest I managed to get was returning “hello” when I replaced 4 with 14, just to see if the length code would work.
Try this:
I’m using a generator expression, it works like this:
sin words usingsplit()n1for each of those that meet the condition1s