Display only words that have an even number of vowels within a sentence

1

I need to make this code in which you scroll through a list and print to the user words that have an even number of vowels. Here is the code:

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)):
  for char in palavras[i]:
    if char in vogais:
        vPalavras += 1
    if vPalavras % 2 == 0:
      print(palavras[i])

So, he actually returns something to me, but it's only the first two words, ignoring "party" and "yesterday".

    
asked by anonymous 29.08.2018 / 21:51

3 answers

2

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)
    
30.08.2018 / 01:54
0

Your mistake is simple.

 if vPalavras % 2 == 0:

This code means that if the word rest 2 equals zero as soon as it prints, magnifica has 4 vowels, so 4% 2 = 0, so it prints. I suggest using the different nomenclature.

 if vPalavras / 2 == 1:

So you guarantee that the division of words is ALWAYS 2 and not an even number

    
29.08.2018 / 22:10
0

Hello! Welcome to Python.

Initially your code creates a list with only a string inside, I suggest to create only the string, because the list is confusing here:

frase = "Foi magnifica a festa ontem."  # removido [] para não criar uma lista

Another suggestion for your whole life in python is to avoid using for i in range(len(palavras)): - for of python already iterates in the direct elements, so you'll rarely need the index at most times what you want is to use for palavra in palavras: and iterate directly in the words and not in the index.

EDITING: According to the comment below, it looks like "pairs of vowels" means "even number of vowels", so just count how many vowels you have in the word.

palavras = frase.split()
for palavra in palavras:
    if numero_de_vogais(palavra) % 2 == 0:
        print(palavra)

def numero_de_vogais(texto):
    return len([letra for letra in texto if letra in vogais])

It has another form using sum :

def numero_de_vogais(texto):
    return sum(1 for letra in texto if letra in vogais)
    
29.08.2018 / 22:12