Why does my code fail when I use append?

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

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.

1 Like

I did mind almost exactly the same way, but there’s one thing you missed. In your for loop, you need to add a break after the return False. Otherwise, if the last word in your list is >= x, it will end up returning True. Since you’re only concerned whether or not ALL words are of a certain length or greater, you can break your loop when you find a single one that’s not.

Not so; it would be unreachable after return. The loop and the function are exited on return, meaning the first short word will result in return False.

This is my answer, wondering is there is a better way to solve it?

# Write your x_length_words function here:
def x_length_words(sentence, x):
  new_sent = sentence.split()
  for i in range(len(new_sent)):
    if len(new_sent[i]) < x:
      return False
    else:
      return True
# Uncomment these function calls to test your tip function:
print(x_length_words("i like apples", 2))
# should print False
print(x_length_words("he likes apples", 2))
# should print True

link for the question:
https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-strings-cc/exercises/x-length-words

Do we need to reference by index? Since we are only polling the elements of the list we could just as easily use,

for word in sentence.split():

and then test if the length is less than x.

Should we be returning True from within the loop? Won’t this give a false positive since it terminates the loop?

1 Like

That is a lot better, I acutely tried to do something close to that but i got an error for comparing int to string.
Thanks a lot

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