Your code is, for very little, almost working.
The first and foremost problem is that you do not restart counting vowels with each word . As you did, the count will accumulate; so instead of considering word for word, you end up always considering the beginning of string .
To solve this, simply add vPalavras = 0
whenever you change words.
frase = ["Foi magnifica a festa ontem."]
palavras = 0
vPalavras = 0
vogais = "aeiouAEIOU"
# separar palavra por palavra
for i in range(len(frase)):
palavras = frase[i].split()
# descobrir quais palavras possuem vogais em par
for i in range(len(palavras)):
vPalavras = 0 # Reinicia o contador a cada palavra
for char in palavras[i]:
if char in vogais:
vPalavras += 1
if vPalavras % 2 == 0:
print(palavras[i])
In this way, the output becomes:
Foi
Foi
magnifica
magnifica
magnifica
magnifica
festa
festa
ontem.
ontem.
ontem.
We have all the words we want, but repeated. Because? Well, notice that the word repeats exactly the same number of vowels that it has. Foi
has two vowels, appearing twice; magnifica
has four vowels, repeating four times. This is because you made the condition if vPalavras % 2 == 0
within the loop that traverses the characters of the word . Thus, for each character, whenever the total of vowels is even, the word is displayed. We do not want this, so just remove an indentation level from this conditional, leaving it within just the loop that runs through the words:
frase = ["Foi magnifica a festa ontem."]
palavras = 0
vPalavras = 0
vogais = "aeiouAEIOU"
# separar palavra por palavra
for i in range(len(frase)):
palavras = frase[i].split()
# descobrir quais palavras possuem vogais em par
for i in range(len(palavras)):
vPalavras = 0 # Reinicia o contador a cada palavra
for char in palavras[i]:
if char in vogais:
vPalavras += 1
if vPalavras % 2 == 0: # Aqui removi um nível de indentação
print(palavras[i])
And with that, the result will be:
Foi
magnifica
festa
ontem.
Eureca! We have the desired result.
But why not other improvements in your code?
You start by defining the phrase:
frase = ["Foi magnifica a festa ontem."]
However, given the presence of the brackets, you are creating a list of strings . If, by chance, you need more phrases, it might even be interesting to do so, but since you only have one, you simply define string :
frase = "Foi magnifica a festa ontem."
To go through all the sentences, you did:
for i in range(len(frase)):
palavras = frase[i].split()
Hardly in Python you will need to range(len(...))
to go through something. This is not readable - and if it is not readable, it is not pythonic . You could very well replace it with:
for frase in frases:
palavras = frase.split()
But since we no longer have a list of strings , you simply do:
palavras = frase.split()
To walk through the words, too, you do not need range(len(palavras))
; a for palavra in palavras
is enough.
for palavra in palavras:
quantidade = 0
for vogal in 'aeiouAEIOU':
quantidade += palavra.count(vogal)
Notice that instead of traversing each character of the word and verifying that it is a vowel, I traced each vowel and counted how many times it appears in the word, adding up to the total. You can even simplify this code using list comprehension :
for palavra in palavras:
quantidade = sum(palavra.count(vogal) for vogal in 'aeiouAEIOU')
This disregarding accented vowels, such as á, ã, é, etc.
So your code could just be:
frase = "Foi magnifica a festa ontem."
palavras = frase.split()
for palavra in palavras:
quantidade = sum(palavra.count(vogal) for vogal in 'aeiouAEIOU')
if quantidade % 2 == 0:
print(palavra)