Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python for loop: IndexError: list index out of range

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading