Use of "for x in range (n, m)"

-1

I am designing a program in python to calculate the sum from n to m of i, and for this I am using "for x in range (n, m)".

print("Este programa calcula la sumatoria desde n hasta m de i.\n\nA 
continuación ingrese los límites:")
print("")

n = int(input("Ingrese un valor para el límite inferior (n):"))
m = int(input("Ingrese un valor para el límite superior (m):"))

if n < m:
   for x in range(n,m):
      sumatoria = n + x:
   print ("el valor de la sumatoria es " + str(sumatoria))
else:
   print("el límite superior debe ser mayor que el inferior.")

Apparently there is a problem with n and x since syntax error appears. Could someone explain to me what is going wrong?

Thank you.

    
asked by Michaelhper 26.09.2017 в 02:35
source

1 answer

2

The truth is that your code has several errors but I help you fix them.

In this first line you have a car jump that you should eliminate:

print("Este programa calcula la sumatoria desde n hasta m de i.\n\nA 
continuación ingrese los límites:")

On the other hand you have two points at the end of this line that cause your syntactic error

sumatoria = n + x:

To finish, your code does not do what you want since you overwrite the value of the summation variable instead of increasing it. To do what you want, you must do the following within your if :

sumatoria = 0
for x in range(n,m+1):
   sumatoria = sumatoria + x
print ("el valor de la sumatoria es " + str(sumatoria))

As you can see, I add 1 to the m within the range function since it does not include the upper limit that I understand you need. That is, the following code:

range(1,5)

would result in:

[1,2,3,4]

Greetings and I hope that my answer has served you.

    
answered by 26.09.2017 в 12:49