Display Qtd of read numbers, medium, larger and lower read value

Asked

Viewed 117 times

-2

I have the following code:

qtdNumeros = int(input())
if qtdNumeros <= 0:
    menor = "Nenhum"
    media = "Nenhuma"
    maior = "Nenhum"
else:
    menor = maior = soma = float(input())
    for proximo in range(1, qtdNumeros):
        valor = float(input())
        soma += valor
        if valor < menor:
            menor = valor
        elif valor > maior:
            maior = valor
    media = soma / qtdNumeros
print("Menor Lido:", menor)
print("Média dos Lidos:", media)
print("Maior Lido:", maior)

Instead of passing a number amount that it will read and display the media, the smaller and larger one, I would like to enter numbers until an empty row is typed, and also, display the amount of numbers that have been read. How can I implement this?

1 answer

0

It’s something like this you want ?

import os

numbers = []

while True:
    os.system("cls") # Esse comando só serve para limpar a tela, então ignore caso você ainda não conheça sobre o módulo "os"

    num = input("\n Digite um número: ")
    if not num: break #Sai do bloco caso o usuário apenas aperte ENTER

    num = float(num) 
    numbers.append(num) # Acrescenta o número à lista de números
    numbers.sort() # Organiza os números na lista em ordem crescente

    # Calcula a média dos números inseridos
    media = 0
    for num in numbers:
        media += num
    media /= len(numbers)

    input("\n Maior número: {} \n Menor número: {} \n Média: {}".format(numbers[-1],numbers[0],media))

Maybe you haven’t learned about lists yet, but what I’ve done with this code is I’ve put all the numbers inside a sequence, in which it will be arranged in such a way that the numbers are in ascending order. After that I just take the value of the first element of the list and the last element of the list. And in relation to the average, I just did the calculation going through and summing up each value on the list and at the end I divided by the amount of elements on the list.

Browser other questions tagged

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