Program to count the vowels of a chain

2
  

"Perform a call_count_name () function that receives as an argument any string and returns the number of vowels present in the string."

def contar_vocales(x):
    voc=0
    for i in len(cad):
        if cad[i]=='a' or cad[i]=='e'or cad[i]=='i' or cad[i]=='o' or cad[i]=='u' or cad[i]=='A'or cad[i]=='E' or cad[i]=='I' or cad[i]=='O'or cad[i]=='U':
            voc=voc+1
        return voc


cad = raw_input('')
print contar_vocales(cad)

When I run it, it says "iteration over non-sequence" on lines 5 and 14.

I tried to solve it in several ways, but I am far from being able to solve it.

Thinking a little more I came to this program, but it does not count the vowels, but it puts:

  

0
  0
  etc

#FUNCIONES

def contar_vocales(x):
    contador=0
    total=0
    for letra in c:
        if 'a' in c:
            contador=+1
        if 'e' in c:
            contador=+1
        if 'i' in c:
            contador=+1
        if 'o' in c:
            contador=+1
        if 'u' in c:
            contador=+1
            total=contador
    return total

#PROG. PPAL

c=str('mi cadena')
print contar_vocales(c)
    
asked by Luu 04.11.2017 в 20:53
source

4 answers

2

You are making a mix of for in with indexing (using range ) to traverse the string. You must choose one way or another, although the most efficient and readable option is to use for in . On the other hand the return must be outside the for . You just have to do:

def contar_vocales(cad):
    voc = 0
    for c in cad:
        if c == 'a' or c == 'e' or c == 'i' or c =='o' or c == 'u' or c == 'A' or c == 'E' or c == 'I' or c =='O' or c == 'U':
            voc=voc+1
    return voc


cad = raw_input('')
print contar_vocales(cad)

Another option is to use in in the conditional, for example:

def contar_vocales(cad):
    voc = 0
    for c in cad:
        if c in "aeiouAEIOU":
            voc = voc + 1
    return voc

Or another more efficient option using sum and iterators and a set:

def contar_vocales(cad):
    return sum(c in {"a", "A", "e", "E", "i", "I", "o", "O", "u", "U"} for c in cad)

Keep in mind that you do not have vowels with accents or other accents.

    
answered by 04.11.2017 / 21:06
source
1

FJSevilla gave an excellent answer that explains the underlying problem, as always. I thought I could complement with 2 more options:


With map() :

We generate an iterator on each vowel with map () , counting each vowel in the text , and then add the total of each vowel.

def contar_vocales(texto):
    return sum(map(texto.lower().count, "aeiouáéíóúü"))


With a regular expression:

Using re for this may be too much, but if it is already used elsewhere in the code, I like how it is. re.findall () returns all matches of the regular expression in the text as one list, which we then have len () .

import re

def contar_vocales(texto):
    return len(re.findall("[aeiouáéíóíúü]", texto, re.IGNORECASE))
  • [aeiouáéíóíúü] is a class of characters. That is, it matches one of the characters included in the brackets.
answered by 04.11.2017 в 22:28
0

Using your style I think you forgot that the return must go outside the for and that the for are used with a range

def contar_vocales(x):
    voc=0
    for i in range(0,len(cad)):
        if cad[i]=='a' or cad[i]=='e'or cad[i]=='i' or cad[i]=='o' or \
        cad[i]=='u' or cad[i]=='A'or cad[i]=='E' or cad[i]=='I' or \
        cad[i]=='O'or cad[i]=='U':
            voc=voc+1
    return voc
    
answered by 04.11.2017 в 21:15
0

I think the best way to implement it is like this

extrn ExitProcess : PROC

.data
    cadena DB 'cadena para sacar vocales', 0
    Completo DB 0
.code

longitud PROC
    pop r15
    push rbp
    mov rbp, rsp
    mov rdi, [rsp]+8
    mov r12, 0

    recorrer:
        mov bl, [rdi]
        cmp bl, 0
        je salir

        cmp bl, 61h ; vocal a
        je sumar

        cmp bl, 65h ; vocal e
        je sumar

        cmp bl, 69h ; vocal i
        je sumar

        cmp bl, 6Fh ; vocal o
        je sumar

        cmp bl, 75h ; vocal u
        je sumar


        inc rdi
        jmp recorrer

    sumar:
        inc r12
        inc rdi
        jmp recorrer

    salir:
        pop rbp
        push r12
        push r15
        ret
longitud ENDP

inicio PROC

    lea r13, cadena
    push r13
    call longitud
    pop rax
    call ExitProcess
inicio ENDP

end
    
answered by 04.06.2018 в 20:28