mardi 24 novembre 2020

Trying to make a loop that checks all of the positions in a list. What I am writing only returns true or false for the first position

So the idea is to create a function that checks the length of each word in a string. For a given number, x, if all the words are greater than or equal to x, return True. Otherwise, return False. For instance, if my sentence is "He wants a new computer" and x = 2, it should return false, because "a" is only one letter. "He eats apples" would return True, because no words are less than 2. My code only seems to run through the first word, and not through the rest of them. Can anyone point out what I am doing wrong? I am using python. Thanks!

def x_length_words(sentence, x): 
  sentence_list = sentence.split()
  for i in sentence_list: 
    if len(i) >= x: 
      return True
    else: 
      return False
print(x_length_words("i like apples", 2))
# should print False, prints False
print(x_length_words("he likes apples", 2))
# should print True, prints True
print(x_length_words("He wants a car", 2))
#should print, False, prints True 
    

Aucun commentaire:

Enregistrer un commentaire