-2

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?

  • 1
    This `stuff == 'E' or 'e' or 'I' or 'i' or other` is wrong. – ForceBru Mar 31 '17 at 20:24
  • See also http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values and http://stackoverflow.com/questions/18212574/why-does-checking-a-variable-against-multiple-values-with-or-only-check-the-fi. – DSM Mar 31 '17 at 20:26
  • 1
    Try this on for size `if text[i].lower() in 'aeiou'` – Patrick Haugh Mar 31 '17 at 20:26

1 Answers1

0

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()
Chad Crowe
  • 1,260
  • 1
  • 16
  • 21