Python for loop: IndexError: list index out of range

Advertisements

I am new with Python,
This is my code:

import numpy as np

x = [0, 0, 0, 0, 5, 0, 10, 5]
y = [4, 4, 4, 4, 9, 4, 2, 9]

def unitvector1 (x,y):
    looplim=(len(x)+1)

    for i in range (0,looplim):
        z=i+1
        alfa = np.arccos((x[i]*x[z])+(y[i]*y[z])) / ((np.sqrt(x[i] ^ 2+y[i] ^ 2) * np.sqrt(x[z] ^ 2+y[z] ^ 2))) 

        beta =+ (180-alfa)

    return (beta)

temp=unitvector1(x,y)

print(temp)

And I am getting this error:

IndexError: list index out of range

I tried to do it with two for loops but it did not work.

Your help is much needed, Many thanks in advance for your help.

>Solution :

looplim=len(x) 
for i in range(looplim-1):
alfa = np.arccos((x[i]*x[z])+(y[i]*y[z])) / ((np.sqrt(x[i] ^ 2+y[i] ^ 2) * 
np.sqrt(x[z] ^ 2+y[z] ^ 2)))  

for each iteration value of "alfa" is nan

Leave a ReplyCancel reply