Can anyone help me find the bug? python!

Asked

Viewed 67 times

-2

in this exercise you need to find the harmonic average but it is giving error (the -1 is to stop the x and the x has to be between 1 and 10 3 and the n is the number of notes.) when it tries to rotate, it appears 0. if you can help me, thank you

media = 0
n = int(input())
while not -1:
    x = int(input())
if 1 <= x <= 10 ** 3:
    media = (n / (1 / x))

print(f'{(media)}')
  • 1

    It seems to me that there is an indentation error in the if command. I don’t know what you want with while not -1: by description I believe that should be while n <> -1:. Since you don’t have another reading of the variable n if it is different from -1 then it will go into infinite loop. From what I understand of harmonic average you would have to read n values make the cumulative of the inverses and at the end, after all the accumulation, divide n by this accumulated.

1 answer

0


Some errors in your code:

  • while not -1: creating an infinite loop.
  • The user could indicate a different number of notes than the amount he was going to pass.
  • Didn’t calculate the average using all the numbers

Basically I had to create a new code that is very simple:

  1. Create sum variable.
  2. Create n variable and ask for the number of notes.
  3. Loop that asks for notes based on variable n, and sums the notes into a sum variable.
  4. Then calculate the average and show it on the screen.
soma = 0

n = int(input('Digite a quantidade de notas: '))

for i in range(n):
    x = int(input('Valor nota: '))
    soma += (1/x)

media = (n / soma)

print(f'{(media)}')
  • thank you so much!!!!!

Browser other questions tagged

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