How to validate whether it is a vowel without writing all the letters of the alphabet

Asked

Viewed 6,745 times

6

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

Note that

vowel("a") must return True

vowel("b") must return False

vowel("E") must return True

The True and False values returned must be bool (boolean)

Tip: Remember that to pass a vowel as a parameter it needs to be a text, that is, it needs to be in quotes.

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"

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

3 answers

17

For this you are far from needing one while... or having all the letters of the alphabet.

You’re also making the mistake that you don’t store the input in a variable, and you don’t need to turn it into a string str(..) for the input() already returns a string by default,

The True and False values returned must be bool (boolean)

then this return, return "True", not correct, you must return without quotation marks or return a string and no Boolean.

That said, you can do it like 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 uppercase, and here we transform into minuscule because the vowels we have are also minuscule.

DEMONSTRATION

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

def vogal(letra):
    vogais = "aeiouAEIOU"
    return letra in vogais
  • 1

    Thanks for the help

4

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 High Box or not. The second takes user input and prints True, for vowels and False for consonants.

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
  • 2

    It works, but it is not recommended to do so in Python. The code is difficult to read and therefore difficult to maintain. Always search for the solution pythonica of the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.