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

Asked

Viewed 149 times

1

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))

1 answer

4

Problem

Hello Uberlan, from what I understand you would like a simple script to calculate some values referring to the numbers typed randomly.

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

The way you’re doing it, it’s not functional acima/abaixo of the average is being checked in the insertion of the values, so without knowing what is necessarily the average, you first have to include the values in order to be able to make the desired checks.

Solution

As a resolution, I have made the following code that answers what you are wanting 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 created 3 vetores to store the values:

  • numbers to receive all values that are typed, including using the method append() - which includes the element at the end of the structure.

  • above_average_numbers to receive the values above or equal to the average

  • below_average_numbers for values below average

In the inclusion loop loop:

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

I added the values requested on array in a standardized way to integer however, you can change if you need to, and I also showed the order of the numbers by i+1 because in the first iteration the i has the value of 0.

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

  • average = sum_numbers / len(numbers) calculate the mean with the values obtained by somathat we performed earlier, along with the size of the vector that we included all the values. len() is a function that returns to us the size of the vector.

We later perform another loop to separate the values:

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

        below_average_numbers.append(number)

I believe this part is easy to understand, we use the local variable numberto obtain the respective value of numbers - this is a programming standard presented for good practices of the python (PEP8) and the number is checked to be greater or less than the mean, thus including in the respective vector.

After that, we show all our results in a clear way, despite the print() have gotten a little big with various 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’ve helped.

  • 1

    Cara helped a lot, I have to make a list of 16 exercises, and I have to learn python practically alone! Vlw!

  • 1

    Good @Uberlantorres, any doubt you have about this same flow, just make a comment on this answer that the moment you see the notification I will assist you. If this answer solved the problem or was useful to find the solution, you can accept it by clicking v(zinho) next to the question. So that other people with the same problem are helped by a validated answer.

Browser other questions tagged

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