User type 15 random numbers, program returns average, numbers larger than average and smaller than average!

2
num = []
soma = 0
media = 0
for i in range(1, 16):
    num = int(input('Digite um número: '))
    soma += num
    media = soma / 15
    if num[i] > media:
        print('Maires que a media {}'.format(num[i]))
    if num[i] < media:
        print('Menores que a media {}'.format(num[i]))
print('Soma {} e Media {}'.format(soma, media))
    
asked by anonymous 02.01.2019 / 21:09

1 answer

7

Problem

Hello Uberlan, so I understand you would like a simple script to calculate some values for randomly typed numbers.

Being shown: soma , média , valores abaixo da média , valores acima da média .

In the way you are doing, it is not functional because checking the%% of% of the mean is being checked when entering the values, so without knowing what the average is necessarily, you first have to include the values to be able to do the desired checks.

Solution

As a resolution, I've made the following code that answers what you want to do.

# criando o array para a inclusão dos 15 números aleatórios
numbers = []

# array para separaçnao dos números maiores e menores que a média
above_average_numbers = []
below_average_numbers = []

# laço de repetição para executar o código identado 15 vezes
for i in range(15):
    numbers.append(int(input('Digite o {}º número: '.format(i+1))))

# realizando a soma dos números digitados
sum_numbers = sum(numbers)

# calculando a média
average = sum_numbers / len(numbers)

# laco de repetição para facilitar a manipulação dos números abaixo da média, e acima da média
for number in numbers:
    if number >= average:
        above_average_numbers.append(number)
    else:

        below_average_numbers.append(number)

# retornos
print('\n* A soma dos valores do vetor {} totaliza o valor de {}!\n* A média dos valores é: {}\n* Temos {} números abaixo da média: {}\n* E temos {} números acima da média: {}'.format(numbers, sum_numbers, average, len(below_average_numbers), below_average_numbers, len(above_average_numbers), above_average_numbers))

Some code information: I made the creation of 3 acima/abaixo to store the values:

  • vetores to receive all values that are entered, including using the numbers method - which includes the element at the end of the structure.

  • append() to receive values above or equal to average

  • above_average_numbers for below-average values

Include repeat loop:

for i in range(15):
    numbers.append(int(input('Digite o {}º número: '.format(i+1))))

I have added the requested values in below_average_numbers in a standard way to array integer, however, I can change them if need be, and I also showed the order of numbers by integer because in the first iteration i+1 value of i .

  • 0 is performing the sum of all values of sum_numbers = sum(numbers) . Because array is a native function of sum() , then why not use it?

  • Python computes the average with the values obtained by the average = sum_numbers / len(numbers) we performed earlier, along with the size of the vector that we include all the values. soma is a function that returns the size of the vector to us.

We then make another loop of repetition to separate the values:

for number in numbers:
    if number >= average:
        above_average_numbers.append(number)
    else:

        below_average_numbers.append(number)

I believe that this part is easy to understand, we use the local variable len() to get the respective value of number - this is a programming standard presented for good practices of numbers ( python ) and PEP8 is checked if it is higher or lower than the average, thus including in the respective vector.

After that, we showed all our results in a clear way, although number got a little big with a lot of information.

print('\n* A soma dos valores do vetor {} totaliza o valor de {}!\n* A média dos valores é: {}\n* Temos {} números abaixo da média: {}\n* E temos {} números acima da média: {}'.format(numbers, sum_numbers, average, len(below_average_numbers), below_average_numbers, len(above_average_numbers), above_average_numbers))

I hope I have helped.

    
02.01.2019 / 22:09