Doubt in Python Ties

Asked

Viewed 39 times

-3

Guys have been breaking their heads here for a while, and in a small detail, how do I display below average values? , I’ve already put the go through the list but not the printa. And also the negative value is being accounted for in the sum.

import copy
tempos = []
soma = 0
qtd = 0
while True:
valores = int(input('Valores: '))
tempos.append(valores)
lista2 = copy.copy(tempos)
soma += valores
qtd+=1
media = soma/qtd
if valores < 0:
    break
print(f'MEDIA: {media}')
for c in range(tempos):
   if tempos < media:
     print(f'{tempos:}')

even if you read not know what it is, but has an idea, comments there

  • print(f'tempo abaixo da media:{c}') that’s it?

  • when editing unintentionally I removed the variable, even putting the C variable gave the error: 'list' Object cannot be Interpreted as an integer

1 answer

0

You commented: [...] also the negative value is being accounted for in the sum.

This happened because as soon as you get the user value, you did the following:

valores = int(input('Valores: '))
tempos.append(valores)

In this case the correct is first you evaluate the user input and only then add it.

valores = int(input('Valores: '))

if valores < 0:
  break

tempos.append(valores)

You also commented: I already put the out to go through the list but not printa

Sorry but I don’t understand why you need to go through everything again with each user input:

for c in range(tempos):
  if tempos < media:
    print(f'{tempos:}')

But before I give you a suggestion for that, I want to let you know which times are a list variable. In this case if you want to go through the items just use you:

for c in tempos:

I have a suggestion that should meet what you need and will become simpler:

tempos = []

while True:
  valor = int(input('Valor: '))

  if valor < 0:
    break

  tempos.append(valor)
  media = sum(tempos) / len(tempos)

  print(f'MEDIA: {media}')

  if valor < media:
    print(f'O valor {valor} está abaixo da média: {media}')

Note that I used sum to have Python add the stored values and Len to count how many items I have in the list.

I put it this way more in order to teach you that there are functions in Python that can help you in these mathematical operations.

I hope I helped you.

  • Ahh I get it, thanks for the help, your algorithm despite being different helped a lot in my logic to develop here

Browser other questions tagged

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