List index out of range


#1



import math
import os

a = []
b = []

def vectora(a):
    n = 2
    suma = 0
    for i in range(n):
        numero = random.randrange(1, 100)
        a.append(numero)
        suma = suma + numero
    return a

def vectorb(b):
    n = 2
    suma = 0
    for i in range(n):
        numero = random.randrange(1, 100)
        b.append(numero)
        suma = suma + numero
    return b


den = (a[0]*b[0]) + (a[1]*b[1])
num1 = float(math.sqrt(((a[0])**2)+ ((a[1])**2)))
num2 = float(math.sqrt(((b[0])**2)+ ((b[1])**2)))
num = float((num1)*(num2))
ang = float(den)/num
print ang
vectora(a)
vectorb(b)


#2

Hi megamaster :slight_smile: ,

This line:

den = (a[0]*b[0]) + (a[1]*b[1])

is trying to access the first and second elements of a and b. But you have defined them as:

a = []
b = []

That is why you are getting the list index out of range error.