#8 AntiVowel : .Replace() Problems


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/antivowel?action=lesson_resume

Hi guys, I’m really having difficulties with with this chapter and am doing my best to not rely on the forums for answers. So, instead of just looking at the straight-up answer, I’m going to ask for help.

<In what way does your code behave incorrectly? Include ALL error messages.>

The code does not throw out any errors. “Hey look words!” will return with “Hey lk wrds!”. I can’t find the pattern to this code. It runs, but I don’t know what it’s doing. “Babinga” will return “bbing”. “Am”, “AAAm”, “am”, and “aaam” will work. Different vowels placed next to each other will only make the code remove the latter letter (“Aam” will return “Am”).

<What do you expect to happen instead?>
The code is supposed to iterate through the text. If a letter in the text is found in the “vowels” string, the code should eliminate the letter.

```

def anti_vowel(text):
vowels = “AEIOUaeiou"
for letter in text:
if letter in vowels:
novowelword = text.replace(letter,”")
print novowelword
return novowelword


#10: Made Two Scripts. Both Seem to Work but Aren't Accepted
#2

replace() method doesn’t change the original string. It just returns the copy of that string where old substring replaced with the new one.

look at here

def anti_vowel(text):
  vowels = 'AEIOUaeiou'
  for letter in text:
    print letter
    if letter in vowels:
      novowelword = text.replace(letter,"")
      print text
      print novowelword
      print text
      print '...........end.............'
  return novowelword
print anti_vowel('Aeiou')

consider how it’s working.After replacing the letter it remains same. so assign the replacing value to text instead of novowelword then return the text .


#3

Thank you! I understand what’s going on now. novowelword was never defined. Below I found two solutions to this problem. Thanks again for the help!

def vowel_slayer1(text):
  vowels = "AEIOUaeiou"
  novowelword = ""
  for letter in text:
    if letter not in vowels:
      novowelword = novowelword + letter
  print text
  print str(novowelword)
  print "---------------------------------------"
  return str(novowelword)

def vowel_slayer2(text):
  vowels = "AEIOUaeiou"
  for letter in text:
    if letter in vowels:
      text = text.replace(letter,"")
  print (text)
  print (text)
  print ("--------------------------------------------")
  
vowel_slayer2("aaAAAm")

#4

I found also a good way to solve the problem, so i post it here.

def anti_vowel(c):
  vow = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
  chars = []
  for i in c:
    if i not in vow:
      chars.append(i)
  return "".join(chars)    

print anti_vowel("Hey I Love YOU")

#5

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