if-else structure does not work

1
#!usr/bin/env python
def vocales(string):
    if "A" or "a" or "E" or "e" or "I" or "i" or "O" or "o" or "U" or "u" in string:
        nvocalesami = (string.count("a"))
        nvocalesama = (string.count("A"))
        nvocalesemi = (string.count("e"))
        nvocalesema = (string.count("E"))
        nvocalesimi = (string.count("i"))
        nvocalesima = (string.count("I"))
        nvocalesomi = (string.count("o"))
        nvocalesoma = (string.count("O"))
        nvocalesumi = (string.count("u"))
        nvocalesuma = (string.count("U"))
        part1 = (nvocalesami + nvocalesama + nvocalesemi + nvocalesema + nvocalesimi + nvocalesima)
        part2 = (nvocalesomi + nvocalesoma + nvocalesumi + nvocalesuma)
        todo = (part1 + part2)
        return ("Hay",todo,"vocales en la frase")
    else:
        return("No hay vocales en la frase")

I have a problem with this code when using the vocal function with an argument without vowels, for example:

Exit

vocales("r")
Hay 0 vocales en la frase

Because it does not come out "There are no vowels in the phrase"?

    
asked by Power 13.12.2016 в 22:55
source

3 answers

2
#!usr/bin/env python
def vocales(string):
 if "A" in string or "a" in string or "E" in string or "e" in string or "I" in string or "i" in string or "O" in string or "o" in string or "U" in string or "u" in string :
    nvocalesami = (string.count("a"))
    nvocalesama = (string.count("A"))
    nvocalesemi = (string.count("e"))
    nvocalesema = (string.count("E"))
    nvocalesimi = (string.count("i"))
    nvocalesima = (string.count("I"))
    nvocalesomi = (string.count("o"))
    nvocalesoma = (string.count("O"))
    nvocalesumi = (string.count("u"))
    nvocalesuma = (string.count("U"))
    part1 = (nvocalesami + nvocalesama + nvocalesemi + nvocalesema + nvocalesimi + nvocalesima)
    part2 = (nvocalesomi + nvocalesoma + nvocalesumi + nvocalesuma)
    todo = (part1 + part2)
    return ("Hay " + str(todo) + " vocales en la frase")
 else:
    return("No hay vocales en la frase")

print vocales("r")

This code works, the problem was that you did not group the expression of the if with parentheses.

    
answered by 13.12.2016 / 23:31
source
0

The problem is that the if is evaluating each or as several individual logical conditions and not as a single compound condition, ie, it is evaluating each operand, and upon finding the first operating as true enters the condition.

You can prove this by leaving only the following condition:

if "A":

And it will be executed in the same way that your code is currently running.

The problem is solved easily by enclosing the conditions in parentheses, as follows:

if ("A" or "a" or "E" or "e" or "I" or "i" or "O" or "o" or "U" or "u") in string:

And there you will get the exit:

$ python vocales.py
No hay vocales en la frase
    
answered by 13.12.2016 в 23:33
0

use the following code

def vocales(string):
    if any(x not in string for x in ('A', 'E',   'I','O','U','a','e','i','o','u')):
    nvocalesami = (string.count("a"))
    nvocalesama = (string.count("A"))
    nvocalesemi = (string.count("e"))
    nvocalesema = (string.count("E"))
    nvocalesimi = (string.count("i"))
    nvocalesima = (string.count("I"))
    nvocalesomi = (string.count("o"))
    nvocalesoma = (string.count("O"))
    nvocalesumi = (string.count("u"))
    nvocalesuma = (string.count("U"))
    part1 = (nvocalesami + nvocalesama + nvocalesemi + nvocalesema + nvocalesimi + nvocalesima)
    part2 = (nvocalesomi + nvocalesoma + nvocalesumi + nvocalesuma)
    todo = (part1 + part2)
    return ("Hay",todo,"vocales en la frases")
else:
    return("No hay vocales en la frase")
    
answered by 14.12.2016 в 00:00