- Code: Select all
`import math`

u = [1, 2]

v = [3, 4]

def length_squared(u):

return sum([a ** 2 for a in u])

def length(u):

return math.sqrt(length_squared(u))

def sub(u, v):

return [a - b for a, b in zip(u, v)]

def scale_by_scalar(u, scalar):

return [a * scalar for a in u]

def dist(u, v):

return length(sub(v, u))

def dist_squared(u, v):

return length_squared(sub(v, u))

def setlength(u, 1):

return scale_by_scalar(u, 1 / length(u))

def norm(u):

return setlength(u, 1)

print norm([100, 0]) # gives [1, 0]

print dist(u, v) # 2.82842712475

print dist_squared(u, v) # 8

I get the following error at def setlength(u, 1);, the error is Expected:).

I was following this tutorial: http://noobtuts.com/python/vector and if you look at the section "Setting the Length of a Vector" it has the code written out as I have. What's wrong?

Thanks.