(Python) Find words with total vowels being even

Asked

Viewed 1,001 times

4

I’d like to ask for your help. I have a code in Python where a part is missing: parse the words of a sentence and save the ones that have an even number of vowels.

Example: "I came home and went to play video games". The word "I arrived" has 4 vowels, 4 is even so I need to keep it in a list.

I want to do this with all the sentences read and then rewrite them inside the list.

Incomplete code:

def frases():
vog="aeiou"
dig="0123456789"
sd=0
sv=0
vp=[]

f = str(input('Digite uma frase: ')).lower()

while f != '':
    vp = f.split()

    for a in range(len(vp)):
        for l in vp[a]:
            if l in vog:
                sv+=1

        for d in vp[a]:
            if d in dig:
                sd += 1

    print('Palavras contidas na frase: {}'.format(len(vp)))
    print('Total de vogais: {}'.format(sv))
    print('Total de dígitos: {}'.format(sd))
    print()

        f = str(input('Digite uma frase: ')).lower()


frases()

3 answers

2

See if that helps you.

def frases():
    vog="aeiou"
    dig="0123456789"
    sd=0
    sv=0
    vp=[]
    matriz_palavras_vogais_pares = []

    f = str(input('Digite uma frase: ')).lower()

    while f != '':

        palavras_vogais_pares = []
        vogais_na_frase = 0
        digitos_na_frase = 0

        vp = f.split()

        for palavra in vp:
            vogais = 0
            digitos = 0
            for letra in palavra:
                if letra in vog:
                    vogais += 1
                if letra in dig:
                    digitos += 1

            vogais_na_frase += vogais
            digitos_na_frase += digitos

            sv += vogais
            sd += digitos

            if vogais %2 == 0: #Ou ainda if (vogais %2 == 0) and (palavra not in palavras_vogais_pares):
                palavras_vogais_pares.append(palavra)

        matriz_palavras_vogais_pares.append(palavras_vogais_pares)

        print('Palavras contidas na frase: {}'.format(len(vp)))
        print('Total de vogais nessa frase: {}'.format(vogais_na_frase))
        print('Total de dígitos nessa frase: {}'.format(digitos_na_frase))
        print('Total de vogais: {}'.format(sv))
        print('Total de dígitos: {}'.format(sd))
        print('Palavras com número par de vogais: {}'.format(palavras_vogais_pares))
        print('Todas as palavras: {}'.format(matriz_palavras_vogais_pares))
        print()


        f = str(input('Digite uma frase: ')).lower()
  • It was almost, the problem is that it adds words that have no vowel. Take the test if you want, example phrase "call 2444-5000", It will add up to the number in the list.

  • @user122202 see now with editing. If you want to add words without repeating them, you can also add and palavra not in palavras_vogais_pares.

  • Thanks for the help. The only problem now is that the list (palavras_vogais_pairs) contains the words with vowels for all phrases. I would need a list for each. Example: list1 with the vowel words of the first sentence. Lista2 with the words of the second sentence.

  • @user122202 search on matrices then.

1

Regex is usually a very useful tool for this sort of thing. As there is already an answer that follows the structure of your code here goes one using only regex and considering only vowels and consonants present in English.

import re
    f = '.'
    while f != '':
        f = str(input('Digite uma frase ou pressione enter para sair: ')).lower()
        print('Palavras contidas na frase: {}'.format(len(re.findall(r'\b\w+\b',f))))
        v_br=r'[aáãâàéeiíoóõúu]'
        c_br=r'[b-dçf-hj-np-tv-z]'
        v2_br = r'(\b(?:'+c_br+r'*'+v_br+c_br+r'*'+v_br+c_br+r'*)+\b)'
        rv = re.compile(v_br, re.UNICODE)
        r2v = re.compile(v2_br, re.UNICODE)
        p2v = re.findall(r2v,f)
        print('Total de vogais nessa frase: {}'.format(len(re.findall(v_br,f))))
        print('Total de dígitos nessa frase: {}'.format(len(re.findall(r'\d',f))))
        print('Palavras com número par de vogais: {}'.format(str(len(p2v))+", "+str(p2v)))
  • Interesting, but for me that I am very beginner got confused kkk. However would you tell me if you have how to create a matrix with indefinite size, as well as the lists? example: list=[] matrix=??

  • Don’t be scared. Regex looks very strange even at first glance. As far as I know in python an array is simply a list list. For neither of the two it is necessary to pre-define the size, but I see no relation of one thing to another...

0

Updated but incomplete code:

def frases():
vog="aáãâàéeiíoóõúu"
dig="0123456789"
sd=0
sv=0
vp=[]
vtp=[]
vtv=[]
vtd=[]
vpp=[]

f = str(input('Digite uma frase ou pressione enter para terminar: ')).lower()
vp = f.split()
vtp.append(len(vp))

for p in vp:
    v = 0
    d = 0
    for l in p:
        if l in vog:
            v += 1
        if l in dig:
            d += 1
    sv += v
    sd += d

vtv.append(sv)
vtd.append(sd)
v=0
d=0
sv=0
sd=0

while f != '':
    f = str(input('Digite uma frase ou pressione enter para terminar: ')).lower()
    vp = f.split()
    vtp.append(len(vp))

    for p in vp:
        v = 0
        d = 0
        for l in p:
            if l in vog:
                v += 1
            if l in dig:
                d += 1
        sv += v
        sd += d

    vtv.append(sv)
    vtd.append(sd)
    v=0
    d=0
    sv=0
    sd=0

for i in range(len(vtp)-1):
    print()
    print('Palavras contidas na {}º frase: {}'.format(i+1, vtp[i]))
    print('Total de vogais na {}º frase: {}'.format(i+1, vtv[i]))
    print('Total de dígitos na {}º: {}'.format(i+1, vtd[i]))
    print()

phrases()

I need to work on that part:

if v %2 == 0 and v != 0 and p not in vpp:
 vpp.append(p)
if len(vpp) > 0:
for i in range(len(vpp)-1):
    print('Palavras com número par de vogais da {}º frase: {}'.format(i+1, vpp[i]))

I would need a list for each sentence. Example: list1 = words with even vowels of the first sentence. Lista2 = words with even vowels of the second sentence. etc....

Browser other questions tagged

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