Chris - 11 months ago 54

Python Question

I am running the below code.

**Can anybody explain to me why the for loop in my code is not running for p=7?** I meant to run the loop len(A)-1 times (omitting 0). I used

`for p in xrange(1,n-1)`

There is the whole routine

`A=[-1, 3, -4, 5, 1, -6, 2, 1]`

P=[-1, 2, -2, 3, 4, -2, 0, 1]

def solution(A):

results=[]

n=len(A)

for p in xrange(1,n-1):

if P[p-1]==P[n-1]-P[p-1]-A[p]:

results.append(p)

return results

Answer Source

`range(a, b)`

(or `xrange`

) iterates from `a`

to `b-1`

included. So the loop with `xrange(1,n-1)`

will run `n-2`

times.

Look at the docs!