Calculate the sum of a range of numbers entered by the user

Asked

Viewed 1,460 times

0

Read 15 numbers typed by the user and calculate the average values between x and y positions (inclusive).

I started trying to do...

numeros = []
for i in range (1, 16):
    numeros.append(int(input(f"Digite o {i} Número: ")))

x = int(input("Entre com o limite Superior 'x':"))
y = int(input("Entre com o limite Inferior 'y':"))

somatorio = 0
for s in range(x, y+1):
    somatorio = somatorio + numeros[s]
print(f"O somatório é: {somatorio}")

But I can’t even make my summation make the right calculation. I know it’s a lot of very basic beginner stuff, but if you can help, I’d appreciate it!

  • 1

    Daniel, the site supports syntax code in the questions, just use the button {} editor. Could [Edit] fix this by posting the code? Already let me know you’re setting somatorio as 0 within the loop and that its upper bound turned the lower and vice versa.

  • 1

    Please do not post the image code. Ask the [tour] and edit the question.

2 answers

4

  1. When using somatorio = 0 within the repeating structure for, your somatorio will always be reset as 0 before adding up the next number. Remove this initialization from within the repeat structure as it should not repeat, but be executed once, before starting the calculation:

    somatorio = 0
    for s in range(....):
    
  2. the function range() works as follows: If you pass 2 parameters, the first parameter is the initial number, and the second is the final number. The way you did, you are asking the user to enter the upper limit and storing in the variable x, but when using the range(x, y) put the x as first parameter! It is inverted. To correct you must switch on one of the two points:

    • Store the limit inferior in x instead of superior:

       x = int(input("Entre com o limite inferior 'x':"))
      
    • Or reverse the call from the range function:

       for s in range(y, x):
      
  3. The same way you did in range(1, 16) to generate 15 numbers, you must put +1 at the upper limit when using the function range(), because it ends one step before reaching the last number. Then it would be: range(x, y+1) (or range(y, x+1) if you reversed the order as suggested in the above item)

  4. Python is much simpler and more didactic than C, it is possible to write this same code in a much more intuitive and efficient way. In the example below, the same code uses only two lines, but remains readable. I will leave here for study and future reference:

    # Já cria a lista diretamente com os números digitados:
    numeros = [int(input(f"Digite o {i} número:")) for i in range(1, 16)]
    # Já soma direto:
    somatorio = sum(
        numeros[int(input('Limite inferior:')):int(input('Limite superior'))+1])
    

0

Opa,
Let me see if I can help you.

If you assigned the upper limit to the variable x and the lower limit to the variable y, when making your loop the range is going from the upper limit (x) to the lower limit(y), it would be interesting to invert the order. Now, in relation to the sum value, you’re assigning 0 to the variable in each loop iteration, so you’re overwriting the sum value every time that piece of code rotates. If you declare the sum = 0 out of the loop, you will probably get the expected result.

Abs,

  • Solved! Thank you all! I learned a lot in just 1 question

Browser other questions tagged

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