Vocal Counter

1

I'm going crazy trying to do the vowel counter, the slogan as it says:

  

Enter 10 sentences. Count and display the number of vowels only of the phrases that contain a period.

They gave us some kind of instructions to do it but I do not understand how to do the program.

The code is as follows:

c = str("Frase con vocales")

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

When executing it does not show anything, it is because it does not have any print , but when you put one "the code is corrupted", but only without the print executes correctly, only that nothing is displayed.

Assuming that the code works correctly, I just need help putting some print , but I'm not sure at all.

Many thanks to those who helped, I had done it before like this:

for i in range(10):
    a = input("Ingrese una frase: ")
    if a.count(".") > 0:
        def contar_vocales(cad):
            voc = 0
            for a in cad:
                if a in "aeiouAEIOU":
                    voc = voc + 1
            return voc
        print("Su frase tiene: ", contar_vocales(a), " vocales")
    elif a.count(".") == 0:
        print("Su frase no tiene puntos")
    
asked by ScarK 08.06.2018 в 23:41
source

2 answers

1

Actually the code works! If you call the function with a phrase inside the paraenteis, changing the return by the print returns a correct result. What I'm missing is calling the function with a phrase as an argument and it works. Hence, what confuses you is that you define c at the beginning but when you define the function you rewrite it, the c of the function has nothing to do with the one above that evaluates the function is what you pass as an argument. Copy code below:

c = str("Frase con vocales")

def contar_vocales(cad):
    voc = 0
    for c in cad:
        if c in "aeiouAEIOU":
            voc = voc + 1
    print (voc)
contar_vocales("hola como andas")
    
answered by 09.06.2018 в 04:42
-1

This is an option, you make a for to obtain the sentences and store them in an array, later you search if this phrase contains "." if so, count the vowels:

#Declara array
frases = []

#Realiza un for para obtener 10 frases
for i in range(10):
    #Pide ingresar frase
    frase = str(input("ingresa la frase: "))
    #almacena frases en array
    frases.append(frase)

#itera frases    
for frase in frases:     
    #busca "."
    if "."  in frase: 
        #Si contiene "." cuenta vocales.
        voc = 0        
        for c in frase:
             if c in "aeiouAEIOU":
                voc = voc + 1       
        print(frase + " contiene " + str(voc) + " vocales")    
    
answered by 08.06.2018 в 23:50