Indicate if a character is a vowel or a consonant

0

Write the vowel function that receives a single character as a parameter and returns True if it is a vowel and False if it is a consonant.

Notice that

  • vogal("a") should return True

  • vogal("b") should return False

  • vogal("E") should return True

The values True and False returned must be of type bool (booleans)

I tried to put this little code but it did not work:

v= str("a"),("e"),("i"),("o"),("u")
def vogal (v):
    if v == ("a")("e")("i")("o")("u"):
        return true
    else:
        return false 
    
asked by anonymous 29.07.2018 / 13:42

2 answers

3

Problems

The returns you have are not right:

return true
...
return false

True and False take the first capital letter. As it has gives execution error because neither true nor false exist.

This comparison is also not right:

v == ("a")("e")("i")("o")("u")

Here it is as if you were using "a" as a function because it makes "a"("e") . And so it does not compare with all the values as you would imagine. Maybe you wanted to do this:

if v == "a" or v == "e" or v == "i" or v == "o" or  v =="u":

That although not very idiomatic would already work.

Solution

The simplest way is to use the in operator by testing with a string that contains only the vowels. This makes your code very simple and easy to read:

def vogal (v):
    if v in "aeiou": # aqui testa com o operador in
        return True
    else:
        return False

Now it gives you the expected result:

print(vogal('e')) # True
print(vogal('b')) # False

See the code running on Ideone

You can even turn the function into a line, although this is usually a style for more experienced programmers:

def vogal (v):
    return v in "aieou"

To also consider the uppercase, you simply enter them in string :

def vogal(v):
    return v in "aeiouAEIOU"

Or use the lower method to always convert the input to lowercase:

def vogal(v):
    return v.lower() in "aeiou"
    
29.07.2018 / 15:14
0

I was able to solve your question with the following code:

def vogal(v):
    vogais = {'a':'a', 'e':'e', 'i':'i', 'o':'o', 'u':'u'}
    try:
        if vogais[v]:
            print('True')
        else:
            print('False')
    except KeyError:
        print('False')

vogal('b')

The code consists of a vowel function and that loads a variable with the vowel dictionary. So, I thought it would be a good assignment to handle exceptions to the code, because if you pass a consonant on the in function vowel, ex: vogal('b') it will return a KeyError error.     

29.07.2018 / 14:50