You were very close :-)
Change this:
angle = atan(float(dy)/float(dx))
For this:
angle = degrees(atan2(float(dy), float(dx)))
The atan2 () function is between what atan () is because it considers the signs on the inputs and goes around the circle:
atan2(...)
atan2(y, x)
Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered
The degrees () function is converted from radians to degrees:
degrees(...)
degrees(x)
Convert angle x from radians to degrees.
, Rich Cody, dy.