FAQ: Code Challenge: String Methods - X Length

I erased the equal sign, and now the code works! thanks for the help!

1 Like

I have a little doubt. In “word” which is called delimiter the quotes(“”) or the data inside it(word). As far as i know, it is the data…am I right.? because in the tutorial it was written like this “delimiter”.join() and we apply this as for example “*” so here * is delimiter? :confused:

Right. The delimiter is always a str since that is the object we are creating. In the case of join and split, I like to refer to the delimiter as a separator string, and let the end quotes act of the string delimiter since the quotes are necessary to identify the data type as str. The two terms are relatively synonymous in this case.

1 Like

Hi, I guess I’m not understanding the difference between a function return vs else statement return

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

print(x_length_words(“id i apples”, 2))

should print False

print(x_length_words(“he likes apples”, 2))

should print True

if I remove the else statement and do the return from the function it works correctly, but if I leave as is they both return as true. Is the else statement looked at before the if statement finishes the for loop? (I also changed what position had the single char word to make sure it was actually looping through all the words and found this result. Gave correct results before because the ‘i’ was first)

sorry I dont know why my post removed my intents for my code. :confused:

#why doesnt the code below work
def x_length_words2(sentence, x):
  w = sentence.split(" ")
  for i in w:
    if len(i) >= x:
      return True
  return False 

Welcome to the forums!

Remember that once a return statement is executed, the function is immediately exited and no further code in the function will be run.

Example

def foo():
  return "bar"
  print("bar") # will never be executed

Additionally, make sure that you are checking if all the words in sentence are longer than x.

1 Like

Here is my short simple solution:

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

why is this wrong:

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

If we are to assume the return after if is inside the loop the it’s fair to say the loop doesn’t run its course. The thing would be to fail the test and return False in the loop, then return True when the loop completes.

Eg.

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

My original code:

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

One question I had, is would it be good practice to include a break in the first part of the if statement upon encountering the first False value rather than have it continue through the list of words?

Pros/cons? anything I’m missing as to why it should or shouldn’t be included?

The break is unreachable after return so can be removed.

I have the same question as the previous poster. To make sure I understand, you are saying that if the loop returns True then the function is exited. So what? It is either true or false. I do not care the order in which the correct answer is found.
It seems only logical that either

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

or

def x_length_words(sentence, x):
  list = sentence.split()
  for i in list:
    if len(i) < x:
      return False
  else:
    return True

should be correct. Please tell me again why the difference matters, if you could.

Thank you!

You’ll figure it out.

Hello!

I am wondering why we just use x in this example as opposed to len(x).
My second question is why do we have to pass in " " for the .split(" ") as opposed to .split().

Thank you for your help with this matter!

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

Please have a look at the following- How do I format code in my posts? - FAQ as formatted code is much easier to read.

You pass x as an argument and it’s already supposed to be an integer value which is used to check the length of a string. In that case len(x) doesn’t really make sense, you already have a length as an integer value.

I don’t know that you do have to pass .split(" ") for this example, it does behave differently than split without arguments, see-https://docs.python.org/3/library/stdtypes.html#str.split but it shouldn’t affect this specific example. Why not test the difference?

This is my solution :heart_eyes:
def x_length_words(sentence,x):

list=sentence.split(’ ')

for word in list:

if len(word)<2:

  return False

  break 

else: return True