FAQ: Code Challenge: String Methods - X Length

This community-built FAQ covers the “X Length” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise X Length

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I was stuck here for a bit using this code:

def x_length_words(sentence, x):
  words = [ ]
  words.append(sentence.split(" "))
  for word in words:
    if len(word) < x:
      return False
  return True

This kept giving me True and True

I couldn’t figure out why it wasn’t right so asked for the solution, which gave me:

def x_length_words(sentence, x):
  words = sentence.split(" ")
  for word in words:
    if len(word) < x:
      return False
  return True

This obviously provided the correct answer of False and True.

I understand why codecademys solution is more concise than my own but can someone explain why these give different outcomes?
Does words = sentence.split(" ") create a list of the words in the sentence or have I got that wrong?

Thanks.

That is exactly what it does. The argument for the split() method (" ") is the separator string that the sentence is split on resulting in a list of words.

1 Like

Thanks for clarifying that!
If that is the case though shouldn’t both of these give the same output?

1 Like

Since split() returns a list, we don’t need to define an empty list to append to.

words = sentence.split(' ');

Aside

Whenever declaring an empty list, use the [] object, not [ ]. It’s moot here, though.

2 Likes

I read through these comments, and I have the same question as driggzzz. I also created an empty list and appended the slices to it. I under why the solution does what it does, but wouldn’t appending to an empty list yield the same results and setting the variable equal to the list that slice() creates?

I just think the messier code (i. e. creating an empty list first) would at the very least return the same output. Since that’s not the case, can anyone explain why?

These challenges all seem to be giving me problems.

can anyone tell me why this doesn’t work?

def x_length_words(sentence, x):
  words = []
  words.append(sentence.split())
  for word in words:
    if len(word) in words >= x:
      return True
  return False

Is this a bit redundant?

Can an integer be found in an array of strings?

Thanks,

I am going to go back and revisit some of the older string lessons. I am getting types messed up in every challenge and its not until someone points out how silly it looks that I understand.

1 Like

I don’t really understand the solution. The first section of code is the answer provided.

def x_length_words(sentence, x):
words = sentence.split(" ")
for word in words:
if len(word) < x:
return False
return True

And this is my solution:

def x_length_words(sentence,x):
new=sentence.split(" ")
for i in new:
if len(i)>=x:
return True
return False

print(x_length_words(“i like apples”, 2))
should print False
print(x_length_words(“he likes apples”, 2))
should print True

My code, returned True and True. But it should return False and True.

Since the length of “i” is less than 2, it will return False, but it returned True.

Any help is appreciated. Thanks.

The for loop in the example show that if one word in that sequence is under it will always be false no matter what.

If you do it your way (with correct indentation when returning false) it will make it true when ever it has one word that meets the requirement, even if there are words not meeting it after that singular word because there is nothing setting back to false.

I tried using your code in this example and had different results to yours. And after tweaking some indentation I found that you might have added an extra indentation on the “return false” line giving it different Boolean expressions that I had mentioned above. But if tweaked to be the right format it would return True and True because there is at least one word that is longer than required.

If you are still confused I can give an example.

Hope this helps!

A link to the exercise would be good to have. Please post it if you can find it again.

x-length-words

2 solutions in driggzzz inital question return different results because in his code he creates a list within a list and then calculates with len() not the length of the words, but the length of the inside list (which is more than 2 in both exercise examples).

I mean:

def x_length_words(sentence, x):
  words = []
  words.append(sentence.split())
  for word in words:
    if len(word) in words >= x:
      return True
  return False

Here {words} equals [[string1, string2, string3 & etc.]], and not [string1, string2, string3 & etc.] like in Solution. Iterating through {words} with len() would count len() of words’ single element (inside list) and logically return quantity of elements ({strings}) in this list, not their own length.

Therefore different results.

Hello everybdoy, after I wrote this code I got true for “print(x_length_words(“i like ii”, 4))”. But when I change the return true and return false with each other, it gives me the correct answer which is false. Is there any way to do this correct with the code mine as below? Or you say, for value returns first true or false when it founds even just 1 ?

wrong one (mine)

def x_length_words(sentence, x):
  words = sentence.split()
  for word in words:
    if len(word) >= x:
      return True
  return False


true one:
def x_length_words(sentence, x):
  words = sentence.split()
  for word in words:
    if len(word) < x:
      return False
  return True

Since we cannot see how your code in indented (which, in Python, is an essential part of the code), we cannot comment without guessing as to your meaning. Please edit, and use the </> icon in the menubar of the box you are typing in to preserve Python format.

Thank you for reminding, that was my first post. You can check it now

1 Like

Ah! Much better!

Remember that return does two things:

  1. It returns a value to the calling statement.
  2. It halts processing of the function. Once return is reached one time, the function is done.

That means that, in your version, as soon as the first word of length greater than or equal to x is found, the function returns True and halts. “Abercromby is a nice store” would return True for x = 5.

Now, you could come up with a way to count up all of the words that pass and then compare that number with the length of the split sentence, but when you stop and think about it, since only one short word is all it takes to make the function return False, the “true one” above is much more concise and to the point.

1 Like

We have been beckoning the posters in this thread to provide a link to the exercise so we have some context within which to work out our reasoning and logic. Please, somebody, post a link to the exercise. Thanks.

What is the objective of this function? To see that all words are of a specific length? Or that all words are less than a specific length, or that all words are greater (or equal) to a specific length?

Yes, that one. Here’s the link

Question: Why could the Wiki-Bot FAQ’s generator not include the link?

1 Like