Reputation: 11
def anti_vowel(text):
new = ''
for char in text:
if char in "aeiou" or char in "AEIOU":
ala = text.replace(char, '')
new = new + ala
return new
print anti_vowel("Hey look Words!")
It returns 'Hey lk Wrds!' which means that the function somehow completely overlooked the "e".
Upvotes: 1
Views: 4634
Reputation: 9680
Strings are immutable in Python. So when you do the following,
ala = text.replace(char, '')
this create a new string replacing char
with empty string and assigns it to ala
. However, the value of text
is still the same original string the function anti_vowel
was passed. After the loop terminates, value of ala
will be the original string with all occurrences of the last vowel removed. What you should do instead is -
def anti_vowel(text):
for char in text:
if char in "aeiouAEIOU":
# update text after replacing all instances of the vowel
# char in text with the empty string
text = text.replace(char, '')
return text
You might also consider using the string method translate
.
def anti_vowel(text):
vowels = "aeiouAEIOU"
return text.translate(None, vowels)
Upvotes: 0
Reputation: 56624
def anti_vowel(text):
new = ''
for char in text:
if char in "aeiou" or char in "AEIOU":
ala = text.replace(char, '')
Let's stop here and look at what happens:
For each character in the message, if the character is a vowel, you make a copy of the original text with that vowel removed. Then you assign it to ala
... but?
new = new + ala
return new
The indentation means that new = new + ala
only runs once - after your for loop is finished. On your sample data, "Hey look Words!", the last vowel you saw was 'o', so ala
contains a string with no 'o's (but all other vowels left alone):
print anti_vowel("Hey look Words!") # => "Hey lk Wrds!"
print anti_vowel("aeiouAEIOU!") # => "aeiouAEIO!"
(Your test string only had two vowel-characters, 'e' and 'o', which meant you couldn't see any difference between "why is the e not removed?" and "why is only the o removed?", which would have been a more obvious indicator of the problem!)
Fixing your code directly would look like
def anti_vowel(text):
result = text
for char in text:
if char in "aeiouAEIOU":
result = result.replace(char, '')
return result
but a more Pythonic approach is:
# a set allows very fast look-ups
VOWELS = set("aeiouAEIOU")
def no_vowels(s):
# keep every character that is not a vowel
return "".join(ch for ch in s if ch not in VOWELS)
print(no_vowels("Look Ma, no vowels!")) # => "Lk M, n vwls!"
Upvotes: 6
Reputation: 44823
The problem is that you are doing the replace on text
every time, not on the updated variable ala
. Try this instead:
def anti_vowel(text):
new = ''
ala = text
for char in text:
if char in "aeiou" or char in "AEIOU":
ala = ala.replace(char, '')
new = new + ala
return new
print anti_vowel("Hey look Words!")
Upvotes: 1
Reputation: 34146
You can instead check if char
is not a vowel. If it is not, then add it to the string:
def anti_vowel(text):
new = ''
for char in text:
if char not in "aeiouAEIOU":
new += char
return new
Note that it would be better to use
if char not in "aeiouAEIOU":
or (as @Michal commented):
if char.lower not in "aeiou":
instead of
if char not in "aeiou" and char not in "AEIOU":
Upvotes: 1