Your code is, by very little, almost working.
The first problem, and the main one, is that you do not restart the vowel count with each word. As you did, the count will accumulate; so, instead of considering word for word, you end up always considering from the beginning of string.
To solve this, just add one vPalavras = 0
every time you change your word.
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. Why? 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 will be displayed. We don’t want that, so just remove a level of indentation from this condition, leaving it within 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 will be creating a list of strings. If you need more phrases, it might be interesting to do this, but since you only have one, you just have to define how string only:
frase = "Foi magnifica a festa ontem."
To go through all the phrases, you did:
for i in range(len(frase)):
palavras = frase[i].split()
Hardly in Python you will need to do range(len(...))
to go through something. That’s not readable - and if it’s not readable, it’s not pythonic. You might as well replace it with:
for frase in frases:
palavras = frase.split()
But since we no longer have a list of strings, just you do:
palavras = frase.split()
To go through the words, too, is not necessary range(len(palavras))
; one for palavra in palavras
suffice.
for palavra in palavras:
quantidade = 0
for vogal in 'aeiouAEIOU':
quantidade += palavra.count(vogal)
Note that instead of going through each character of the word and checking if it is a vowel, I went through each vowel and counted how many times it appears in the word, adding to the total. You can even simplify this code using comprehensilist on:
for palavra in palavras:
quantidade = sum(palavra.count(vogal) for vogal in 'aeiouAEIOU')
This disregarding accentuated vowels, such as á, ã, é, etc.
So your code could be just:
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)
wow, just start the variable... thank you so much for all the tips! ;)
– H. Lamarr