How to define a vowel without writing all the letters of the alphabet

3

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

Notice that

Vowel ("a") should return True

Vowel ("b") should return False

Vowel ("E") should return True

Returned True and False values must be of type bool (Booleans)

Tip: Remember that to pass a vowel as a parameter it must be a text, ie it must be enclosed in quotation marks.

vogais = ("a", "e", "i", "o", "u")
letra = ("b", "c", "d", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")

def vogal():
    input(str("Digite uma letra: "))
    while letra == vogais:
        vogal = "a" or "e" or "i" or "o" or "u"
        return "True"
    else:
        return "False"

No error in the code, but it is not in the right format, what can I do to make it simpler?

    
asked by anonymous 06.04.2017 / 09:33

3 answers

16

For this you are far from needing a while ... or have all the letters of the alphabet.

You're also making the error that you do not save the input to a variable, and you do not need to convert it to string str(..) because input() already returns a string by default,

  

Returned True and False values must be of type bool (Booleans)

Then this return, return "True" , is not correct, you should return without the quotation marks unless you return a string and not Boolean .

That said, you can do this:

def vogal(letra):
    vogais = "aeiou"
    if letra.lower() not in vogais: # verificar se a letra digitada minuscula nao existe na nossa variavel vogais
        return False
    return True

letra = input("Digite uma letra: ")
print(vogal(letra)) # aqui imprime True ou False

Notice that letra.lower() is to cover the hypothesis that the typed letter can be capital, and here we transform into a minuscule because the vowels we have are also in the lower case.

DEMONSTRATION

The function could even be reduced (with the alternative without the function lower() ) to:

def vogal(letra):
    vogais = "aeiouAEIOU"
    return letra in vogais
    
06.04.2017 / 10:29
3
def isVogal(letra):
    vogais = 'aeiou'
    return letra.lower() in vogais

def vogal():
    print(isVogal(input("Digite uma letra: ")))


vogal()

I created 2 methods, where the first one returns True for vowel and False otherwise, regardless of whether the box is high or not. The second receives the user input and prints True, for vowels and False for consonants.

    
11.06.2017 / 02:03
2
def vogal(x):
    if x == "a" or x == "e" or x == "i" or x == "o" or x == "u" or x== "A" or x == "E" or x == "I" or x == "O" or x == "U":
        return True
    else:
        return False
    
26.04.2017 / 21:00