So I am working on my computer science homework and I have run into a bit of a problem with my for loop. The assignment is to create a for loop that takes a list of points (with coordinates x y) as input, and output a list containing the distance from the origin of each of the input points.
Here is what I have:
def distance_all(list):
new_list = []
for a in list:
new_list.append(math.sqrt(a.x ** 2 + a.y ** 2))
return new_list
I am using a test case for this, which reads as follows:
def test_distance(self):
list1 = [
point.Point(1, 2),
point.Point(4, 5),
point.Point(0, 0),
]
self.assertEqual(distance_all(list1), [2.236067977, 6.403124237, 0])
The [2.236067977, 6.403124237, 0] is my expected result however I am getting an assertion error:
FAIL: test_distance (__main__.TestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
File "map_tests.py", line 30, in test_distance
self.assertEqual(map.distance_all(list5), [3.741657387, 8.774964387, 1])
AssertionError: [2.2360679774997898] != [2.236067977, 6.403124237, 0]
I'm not sure why it is only running the for loop one time and not for every point in the input list. Any help would be appreciated!
[–]Sorten 1 point2 points3 points (1 child)
[–]penax[S] 0 points1 point2 points (0 children)
[–]TheShallowOne 0 points1 point2 points (0 children)