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
else:
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
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?
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.