(Python) Find words with total vowels being par

0

Hello, I would like to ask your help. I have a python code where it is necessary to analyze one thing: analyze the words of a sentence and save the ones with an even number of vowels. Example: "I got home and went to play video games". The word "I came" has 4 vowels, 4 is even so I need to save it in a list. I want to do this with all the sentences read and then write all the words in 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()

phrases ()

    
asked by anonymous 12.08.2018 / 16:25

3 answers

0

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

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)))
    
12.08.2018 / 18:58
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 improve this 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 vowels par of the 1st sentence. list2 = words with vowels par of the 2nd sentence. etc ...

    
12.08.2018 / 21:29
0

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()
    
12.08.2018 / 16:48