Anti_vowel function doesn't work. Any help?


#1
def anti_vowel(text):
  vowels = ["a", "A", "e", "E", "o", "O", "i", "I", "u", "U"]
  for char in text:
    new_text = []
    if char not in vowels:
      new_text.append(char)
      "".join(new_text)
    return new_text

it returns [‘h’] but I don’t get why it stops there and doesn’t continue with checking the other characters in the text.


#2

by default a function returns None at the end of the function

If we want to return something else at the end of the function, we can use the return keyword.

So, when a return keyword is reached, the function ends (given return is always at the end of a function)

in your case, return will end the function in the first iteration of the loop (return will force the loop to break so the function can end)


#3
def anti_vowel(text):
  vowels = ["a", "A", "e", "E", "o", "O", "i", "I", "u", "U"]
  new_text = []
  for char in text:
    if char not in vowels:
      new_text.append(char)
      "".join(new_text)
  return str("".join(new_text))

Thanks! with your help I found my main problem and was able to fix the rest. Any more tips on this code?
Ps. How do you get the nice code-formatting when posting here?


#4

why would you place "".join(new_text) inside the (body of the) loop? We only need to join the list into a string after the loop


#5

I shortened the vowels list by switching all the characters to lowercase for the comparison:

def anti_vowel(text):
  vowels = ["a", "e", "i", "o", "u"]
  new_text = []
  for char in text:
    if char.lower() not in vowels:
      new_text.append(char)
  "".join(new_text)
  return str("".join(new_text))

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.