Python input problem

Asked

Viewed 47 times

0

My program is working correctly for a test case but when more than one error occurs shown in the image. The following code:

alfabeto = dict()
entradas = input()
i = 0
for i in entradas:

    texto = input().strip()
    texto = texto.lower()
    texto = texto.replace(' ', "")
    ''' Outra forma de ser feito linha acima
    texto = ''.join(texto.plit())
    ''' 

    for letra in texto:
        if letra not in alfabeto:
            alfabeto[letra] = 1
        else:
            alfabeto[letra] = alfabeto[letra] + 1

    maior = 0
    for chave, valor in alfabeto.items():
        if valor > maior:
            maior = valor
    resultado = []
    for chave, valor in alfabeto.items():
        if valor == maior:
            resultado.append(chave)

    print(''.join(sorted(resultado, reverse=False)))
    alfabeto.clear()    

input used:

3
Computers account for only 5% of the country's commercial electricity consumption.
Input
frequency letters

  • 1

    I never got around to analyzing the code, but at first I think for i in entradas should be, in fact, for i in range(int(entradas))

  • Apart from what Anderson said you can delete the variable i=0 pq it is not used in any part of that code snippet

1 answer

0


With these changes it runs more than once, but what you are wanting to get with logic because I believe that you can make things smaller using the functions Chr() and Ord(), I don’t know if it’s intentional or not that besides removing and joining the spaces if repeat a letter it ignores the whole sentence printa the repeated letter, if it’s not from a warning that I help tidy up.

alfabeto = dict()
entradas = int(input("digite o numero de entradas"))
for i in range(0,entradas):

    texto = input("digite a frase").strip()
    texto = texto.lower()
    texto = texto.replace(' ', "")

    for letra in texto:
        if letra not in alfabeto:
            alfabeto[letra] = 1
        else:
            alfabeto[letra] = alfabeto[letra] + 1

    maior = 0
    for chave, valor in alfabeto.items():
        if valor > maior:
            maior = valor
    resultado = []
    for chave, valor in alfabeto.items():
        if valor == maior:
            resultado.append(chave)

    print(''.join(sorted(resultado, reverse=False)))
    alfabeto.clear()   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.