Your else
should be indented to the same level as your if
and it should have a colon after it else:
. Your code doesn't really seem to do what your question says it should either.
You don't need to iterate over anything in your function because the index is already being passed in as a parameter to the function. You also have your test slightly backwards. Your code is attempting to find if the list vowel
is inside the string w.lower()
which it obviously isn't.
This version makes more sense:
def vowel(w,i):
vowel_list = ['a','e','i','o','u']
if w[i].lower() in vowel_list:
print(True)
else:
print (False)
s = "hellO World!"
vowel(s,0) #false
vowel(s,4) #true
vowel(s,7) #true
Note that it is much nicer to return the values True
and False
from the function instead of printing out directly. For example with this approach we have a simple way to define a function that checks if something is a consonant.
A consonant is simply something which is in the alphabet and which is not a vowel. Python already has a way of checking if something is in the alphabet with the str.isalpha()
method. So we can use this:
def is_vowel(w,i):
if w[i].lower() in ['a','e','i','o','u']:
return True
else:
return False
def is_consonant(w, i):
return w[i].isalpha() and not is_vowel(w, i)
string = "Hello World!"
print(is_vowel(string, 0))
print(is_consonant(string, 0))
print(is_vowel(string, 1))
print(is_consonant(string, 1))