X Length - Why doesn't my code work?

Hello, I’m doing the CODE CHALLENGE: STRING METHODS and I got a question on the task X length.

Sorry for might be a stupid question and I tried to search online and here in the forum before I posted. Hopefully someone can help me!

Question:

  • Create a function called x_length_words that takes a string named sentence and an integer named x as parameters. This function should return True if every word in sentence has a length greater than or equal to x.

This is my code:

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

This is the answer you get if you ask for the solution:

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

I am wondering why my code doesn’t work? Am I using if and else wrong?

Thank you in advance!

1 Like

Hello @system1907986760. I think why your code does not work is this:

You are saying if the length of word is less than or equal to x. But the question asks for greater than or equal to. So you are saying return false if len(word) is equal to x, but the question only wants you to return false if len(word) is less than x. I hope this helps!

3 Likes

As per the guidance by @codeneutrino I’d make sure you were catching the correct word length. I’d also double check your current logic set-up. What would happen if the last word was of a length greater than or equal to x? This would set re as True regardless of any preceding words. Does that still meet the requirements?

1 Like

@ system1907986760
There are two reasons why your code doesn’t work. First run this example test case.

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

w = x_length_words(“my name is john”,3)

print w

notice that this test outputs True, but it should output False because the words “my” and “is” are both less than 3!
1st reason.
now looking at your code “len(word) <= x”, should be “len(word) < x” because we only output False if the length of the word is less than x.
2nd reason:
The method of returning True or false is wrong! when you store a data value ( a string, boolean or integer) as a variable, if you reassign another data value to it, the old data value is replaced with the new one.
look at the example code above to understand this:
when the loop is at “my”, re = False.
when the loop gets to “John”, re= True.
Since True was the last value, it wrongly returns True instead of False.
and one last thing, for purpose of optimization your code should stop running once it finds a word with a length less than x, but yours proceeds to keep checking !

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

You can also use a “while” loop if you want:
def x_length_words_2(sentence,x):
sentence_list = sentence.split()
list_length = len(sentence_list)
word = 0
while word < list_length and len(sentence[word])> x:
word += 1

if word +1 == list_length:
    return True
else:
    return False
1 Like

@system1907986760 system1907986760
Also if you’re bent on using a variable to store your boolean, you can do this:

def x_length_words_3(sentence,x):
re = 1
sentence_list = sentence.split()
for word in sentence_list:
if len(word) < x:
re = False
break
if re == 1:
return True
else:
return re
An initial value of “re” has to be set incase no word in the list is less than x,else when we do
“if re ==1” you’ll get an error that “re” is undefined.
I used re =1, but be careful 0 is seen as False in python and any number higher than zero is seen as True.
Also I added the “break” function which stops the for loop from searching once a word’s length is less than x, like i said this is important for optimization. This is particularly important to me since I do competitive programming where you have large datasets with an execution time limit.
I hope this helps :slightly_smiling_face:

2 Likes

@system1907986760


I noticed my code didn’t indent as I wanted,I’m new to this forum so here is a screenshot

1 Like

Hello @ruronite, welcome to the forums! To indent code, press this button:

, and when this comes up:
, put your code in between the back-ticks. Very good answer, though!

Thank you @codeneutrino, @ruronite and @tgrtim for the help and feedback!

I read all of the replies and understand now why it didn’t work and what I need to think about in the future :smiley:

2 Likes

@codeneutrino oh Thanks I’ll do that next time :slightly_smiling_face:

you’re welcome! happy coding