Display number that most repeated and its quantity

Asked

Viewed 390 times

0

I have the following code:

def repetido(numeros, count):
    for i in range(len(numeros)):
            count.append(float(numeros[i]))
    for x in range(len(count)):
        aux = count.count(count[x])
        if aux > count:
            repet = count[x]
            contador = count.count(repet)
    print("Valor que mais ocorreu:",repet,"que foi encontrado:",contador,'vezes(es)')

#Programa Principal
entrada = input().split()
n = []
if len(entrada) == 0:
    print('nenhum número foi lido!!!')
else:
    print(repetido(entrada, n))

When running the program and give enter, without entering any number, the correct message is displayed.

When I type, for example 1 2 3 2 2 2 2 3, it gives error and does not display what it should display.

I wish I knew where I was wrong.

Thank you!

  • if aux > count, you are comparing an integer number with a list. What was the purpose of this line?

1 answer

1


Friend, besides the aux > count as @Anderson commented, in its code

else:
    print(repetido(entrada, n))

You ask to print the return of the repeated function() but it is not returned anything. Apart from these two things, maybe you could simplify your code using mode.

import statistics 
from statistics import mode 

def repetido(numeros):
    return(mode(numeros))

#Programa Principal  
entrada = input().split()
n = []
if len(entrada) == 0:
    print('nenhum número foi lido!!!')
else:
    print('O valor que mais repetiu foi ',repetido(entrada))

The mode will return the most repeated value, to see more about it take a look at the documentation here

Browser other questions tagged

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