Make a Program that checks if a typed letter is vowel or consonant

Asked

Viewed 2,202 times

-1

alfa = input("Informe uma letra ou consoante:")

if alfa =="a":
    print("Vogal")
elif alfa == "e":
    print("Vogal")
elif alfa == "i":
    print("Vogal")
elif alfa == "o":
    print("Vogal")
elif alfa == "u":
    print("Vogal")
    print("Insira uma letra ou consoante")
else:
    print("Consoante")

How do I make it accept vowel or consonant only. Because if the user gives a number the program will say it is a consonant.

1 answer

0

You can use the string method .isalpha(). Ex.:

alfa = input("Informe uma letra ou consoante:")

if alfa.isalpha():
    if alfa =="a":
        print("Vogal")
    elif alfa == "e":
        print("Vogal")
    elif alfa == "i":
        print("Vogal")
    elif alfa == "o":
        print("Vogal")
    elif alfa == "u":
        print("Vogal")
        print("Insira uma letra ou consoante")
    else:
        print("Consoante")
else:
    print("Não é uma letra!")

So your code should work! Notice the use of the alpha method.isalpha(). The python documentation is quite complete, always try to give it a read when in doubt!

Link to string documentation

Browser other questions tagged

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