0

I'm trying to write a definition where w is a word (either capital or lower case) and i is the index in which a letter in the word is, and the output to be either True or False. For instance,

>>>vowel(hello,1) 

to return True because e is a vowel at the 1th character

So far I have,

def vowel (w,i):
    vowel=['a','e','i','o','u']
    for i in range(0,len(w)):
        if vowel in w.lower():
            print(True)
            else print (False)

And it keeps returning SyntaxError: invalid syntax

Any hints? and thank you in advance!!

ForceBru
  • 43,482
  • 10
  • 63
  • 98
python_newbie
  • 113
  • 1
  • 2
  • 8

2 Answers2

1

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))
or1426
  • 929
  • 4
  • 7
  • Is it the i part that doesn't do what I want it to? Because I can't seem to figure out how to write an index within a definition for the life of me. – python_newbie Sep 17 '15 at 16:19
  • @Nikki I've edited my answer. There were a couple of issues but the loop in your function was one of them yes. – or1426 Sep 17 '15 at 16:26
  • Thank you so much! I knew there had to be an easier way than what I was doing. – python_newbie Sep 17 '15 at 16:34
  • 1
    @Nikki I wrote up my answer to mimic the output you seemed to want from your version but I think it would be much nicer for `vowel` to return either `True` or `False` and allow the caller to print that or do whatever they want with it. – or1426 Sep 17 '15 at 16:38
  • `vowel(s,20)` results in `IndexError: string index out of range` – Josh J Sep 17 '15 at 16:54
  • @JoshTriiJohnston As it should if `s` has fewer than 21 characters. Calling the function with a number larger than the highest valid index of the string is a bug on the part of the calling code. The caller of the function can wrap it in a `try` if they like. – or1426 Sep 17 '15 at 17:01
  • @JoshTriiJohnston: That seems legit. If you don't want that you could use exception handling or just `if w[i:i+1].lower() in vowel_list:`. – Matthias Sep 17 '15 at 17:02
  • The list is not needed. You can use `if w[i].lower() in 'aeiou':` instead. – Matthias Sep 17 '15 at 17:04
  • @Matthias: Yes I know. My intention was to fix the bugs in OP's code without attempting to change anything else so it was as easy as possible to understand why the changes were necessary. – or1426 Sep 17 '15 at 17:07
  • or1426 and Matthias I was just pointing it out in case it wasn't obvious to the OP. I did not say it was incorrect behavior but it is something that OP will need to account for either internally or externally. – Josh J Sep 17 '15 at 17:20
  • Is there anyway I can define the word consonant within the definition of a vowel? – python_newbie Sep 17 '15 at 18:12
  • @Nikki. Yes this is very easy if you define the function `vowel(word, i)` to `return True` or `return False` as applicable. This is one reason I and others suggested that approach. I'll edit my answer to show how as writing code in comments isn't very nice. – or1426 Sep 17 '15 at 22:29
0

Here's what I would do to achieve your goal:

def vowel(word, i):
    if word[i].lower() in ['a', 'e', 'i', 'o', 'u']:
        print(True)
    else:
        print(False)

Hope that helps! :)

Nihdez
  • 33
  • 1
  • 1
  • 6