I am trying to save the vowels in the variable "vowel" separating them by commas, but python takes it as a complete text.
letra = input(str("Introduzca una letra: "))
vocal = "a","e","i","o","u"
def abc(letra,vocal):
if letra == vocal:
print("True")
else:
print("False")
abc(letra,vocal)
How can I get the variable to take the letters separately?
the trick is to use the special "in" operator instead of the "==" comparison operator and to make the "vowel" variable a tuple (you can also do it with a str).
If you used your code when putting aeiou it would come out TRUE, but in AEIOU FALSE. Also in my code it would pass to TRUE, but to FALSE. This happens because it is case sensitive, it adds the vowels in capital letters to the tuple so that this does not happen.
The code can be corrected and reduced:
The function reduces the letter to lowercase and then uses the "in" operator to check if the letter is part of the sequence "aeiou"
A sequence can be a list, a tuple, and also a string.
At the end, the function returns
True
orFalse
depending on whether the letter is a vowel or not.It is bad practice for functions to directly print the result; that prevents its use in other situations where you don't want to display the result.
show
You don't need to do either
str("Introduzca una letra: ")
, since the text is already a string.produces: