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

0
  

Read 15 numbers entered by the user and calculate the mean of the values between the 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 not even make my sum do the right calculation. I know there are many basic beginner things, but if you can help I thank you right away!

    
asked by anonymous 16.10.2018 / 20:01

2 answers

4
  • When using somatorio = 0 within the for repeat structure, its somatorio will always be reset to 0 before adding the next number. Remove this initialization from within the repeat structure as it should not be repeated, but rather be executed once before starting the calculation:

    somatorio = 0
    for s in range(....):
    
  • The range() function works as follows: If you pass 2 parameters, the first parameter is the start number, and the second is the final number. In the way you did, you are prompting the user to enter the upper limit and storing it in the variable x , but at the time of using range(x, y) put x as the first parameter! It's inverted. To fix you should swap in one of the two points:

    • Store the lower border in x instead of top :

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

       for s in range(y, x):
      
  • Just as you did in range(1, 16) to generate 15 numbers, you must put +1 at the upper bound by using the range() function because it ends one step before reaching the last number. Then you would be: range(x, y+1) (or range(y, x+1) if you inverted the order as suggested in the above item)

  • Python is much simpler and didactic than C, it's possible to write that same code much more intuitively and efficiently. In the example below, the same code uses only two lines, but remains readable. I will leave here as a 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])
    
  • 16.10.2018 / 20:21
    0

    Opa,
    Let me see if I can help you. Home If you assigned the upper bound to the variable x and the lower limit to the y variable, when making your loop the range is going from the upper bound (x) to the lower (y), it would be interesting to invert the order. Now, relative to the sum value, you're assigning 0 to the variable at each iteration of the loop, so you're overwriting the sum value every time that piece of code rolls. If you declare the sum = 0 out of the loop, you probably have the expected result.

    Abs,

        
    16.10.2018 / 20:24