Why do not you print the number of capitals of the chain?

1

The program must return the number of capitals that a string entered by the user has.

def eva_cadena():
    contador = 0
    cadena = input("ingrese una cadena: ")
    for i in cadena:
        if i == i.upper():
            contador += 1
    print(contador)
    
asked by José Ignacio 27.07.2018 в 23:24
source

1 answer

1

Because you're using upper() instead of isupper()

upper() converts to uppercase, it is not a data check.
isupper() checks whether something is capitalized or not.

Update: The efficient way to do that is not to compare the letter to something else, but to check if the letter is simply upper case.

def eva_cadena():
    contador = 0
    cadena = input("ingrese una cadena: ")
    for i in cadena:
        if i.isupper(): # No hace falta convertir nada.
            contador += 1
    print(contador)

Update 2: In case it is easier to understand without using a function, this would be similar to:

print(sum([i.isupper() for i in input("ingrese una cadena: ")]))
    
answered by 27.07.2018 в 23:25