What is the logic behind returning true or false first?

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

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.

2 Likes

This will, as mentioned above, return True when the loop may not have completed iterating the entire sentence.

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

Is short-circuiting the loop on purpose. Since a word has been found that is shorter than required, there is no need to continue the loop. Just return False and exit.

1 Like

While studying the code given by the solution, I noticed something interesting.
In the second print statement print(x_length_words("he likes apples", 2)),
if we add one more (or even two/three) whitespace(s) between let’s say the words “likes” “apples”, the result becomes False .
I suppose this is due to the fact in method .split(" ") we used earlier in the code, the delimiter is a “single” whitespace. So when I add to the argument in print statement another whitespace , this second whitespace is somewhat considered as a temporary variable ‘word’ (although it’s an “invisible word”) whose length (len) is 1 (< 2).
Or is there something else happening here?

Consider,

>>> "*          *".split(' ')
['*', '', '', '', '', '', '', '', '', '', '*']
>>> ' '.join(['*', '', '', '', '', '', '', '', '', '', '*'])
'*          *'
>>> 
1 Like

Hi.

I got stuck at the same exercise. Could you please explain why wouldn’t the function return True every single time because it is not a part of any if statements. Thanks

Assuming you are referring to this section of code-

...
    for word in words:
        if len(word) < x:
            return False
    return True

Just remember how return works. As soon as it is executed the function exits.

If this isn’t your issue you may have provide an example/code snippet of the problem you have. For details on code formatting see this FAQ.