Why Does Every Letter in my String get Replaced Instead of Vowels?
Question:
I have the following code:
def replace_vowel(text):
text = list(text)
for i in range(0, len(text)):
if text[i] == 'A' or 'a' or 'E' or 'e' or 'I' or 'i' or 'O' or 'o' or 'U' or 'u':
text[i] = ''
print ''.join(text)
return ''.join(text)
replace_vowel("HeylookWords!")
The code is supposed to replace every vowel, but instead, every letter gets replaced with ”. Why does this happen?
Answers:
Do range(len(text))
And you need == for each comparison. You can’t just do text[i] == ‘A’ then a bunch of or statements.
if text[i] == ‘A’ or text[i] == ‘a’ or… ect
Note that you can also do replace in python, 'some string'.replace('with something','for something else')
If you had a long list of checks you would put your letters into a list and check against the list.
l = ['A','B','C','D']
for letter in l:
if text[i] == letter:
do_something()
I have the following code:
def replace_vowel(text):
text = list(text)
for i in range(0, len(text)):
if text[i] == 'A' or 'a' or 'E' or 'e' or 'I' or 'i' or 'O' or 'o' or 'U' or 'u':
text[i] = ''
print ''.join(text)
return ''.join(text)
replace_vowel("HeylookWords!")
The code is supposed to replace every vowel, but instead, every letter gets replaced with ”. Why does this happen?
Do range(len(text))
And you need == for each comparison. You can’t just do text[i] == ‘A’ then a bunch of or statements.
if text[i] == ‘A’ or text[i] == ‘a’ or… ect
Note that you can also do replace in python, 'some string'.replace('with something','for something else')
If you had a long list of checks you would put your letters into a list and check against the list.
l = ['A','B','C','D']
for letter in l:
if text[i] == letter:
do_something()