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:

Learn Python 3

FAQs on the exercise X Length

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!

11 posts were split to a new topic: Why does my code fail when I use append?

9 posts were split to a new topic: What is the logic behind returning true or false first?

3 posts were split to a new topic: FAQ doesn’t link to the lesson

In the following code, why is it returning only false once or true once? Shouldn’t it return either true or false for each of the words in for loop? Doesn’t it mean that it’s going through all the words in a sentence and finally returning one value? But it doesn’t agree with the indentation level.

def x_length_words(sentence, x): 
  hello= sentence.split(' ') 
  for words in hello: 
    if len(words) < x: 
      return False
      return True
# Uncomment these function calls to test your  function:
print(x_length_words("i like apples", 2))
# should print False
print(x_length_words("he likes apples", 2))
# should print True

Because the return is inside the loop.

The answer given for the exercise is:

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

When I use print(x_length_words(“a bbbb ccccccc”, 2)), it returns False.

But, since the loop will iterate through each word, and the last word has more than 2 characters, shouldn’t it return True? Why is it returning False, which is the result of the firs iteration, and not the last one?

It returns False on the first iteration since the length of the string is less than 2.

And how does it know that the result of the first iteration is the one that should be returned?

It knows by the directions we’ve given in the program.

x_length_words("a bbbb ccccccc", 2)

The length of 'a' is 1, which is less than 2.

So I can conclude that once the condition from if statement is reached the loop stops and it will return the last value?

Not the last value, but the False that is included in the return statement.

How do you account for punctuations?

Please give us an example. In the case of string, the quotes are delimiters and not part of the data. However when there are quotes or apostrophes in the string they are part of the data.

def x_length_words(sentence, x):
  for word in sentence:
    if len(sentence) < x:
      return False
  return True

trying to see why this isn’t working.

That needs to be assigned to a variable.

On the other hand it could be used directly in the loop…

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

my new code, yet it returns the same answer as before: True for both statments.

Should you be checking the word length, not sentence length?

checking word length returns False for both in the terminal.

Less than? Or less than or equal to?

if len(word) < x: