-1

i am trying to find the perimeter of 10 point polygon with given coordinates.

This is what ive got so far

however keep getting an error

poly = [[31,52],[33,56],[39,53],[41,53],[42,53],[43,52],[44,51],[45,51]]
x=row[0]
y=row[1]

``def perimeter(poly):
    """A sequence of (x,y) numeric coordinates pairs """
    return abs(sum(math.hypot(x0-x1,y0-y1) for ((x0, y0), (x1, y1)) in segments(poly)))

    print perimeter(poly)
jono
  • 1
  • 1
  • 2
  • Looks like an 8-point polygon. What is `segments()`? Please post all relevant code. Also -- why the `abs`? Distances are already positive. Also -- your indentation seems wrong. That final `print` shouldn't be indented. – John Coleman Oct 13 '16 at 23:32
  • im very new to python, ive made those changes but i dont know how to define the list elements as x and y for poly[x,y] def perimeter(poly): """A sequence of (x,y) numeric coordinates pairs """ return (sum(math.hypot(x0-x1,y0-y1) for ((x0, y0), (x1, y1)) print (perimeter(poly)) – jono Oct 13 '16 at 23:41
  • What error are you getting? What line? Merely saying that you keep getting an error is uninformative. What is `segments()`? Without seeing that code it is hard for anyone to say anything (beyond the indentation issue). – John Coleman Oct 13 '16 at 23:41
  • I am getting the error when defining for poly[x,y] I am looking to define the points in my list as x and y so they can i can then use them in an equation. the segements fuction has now been removed – jono Oct 13 '16 at 23:44
  • If function `segments()` has been removed then that code would give you an error for trying to use that function (in your `return` line). Please show us the entire traceback for your error. – Rory Daulton Oct 13 '16 at 23:47
  • Traceback (most recent call last): File "python", line 5 for poly[x,y] ^ SyntaxError: invalid syntax I have edited the code so segements isnt apart of the return or the equation. – jono Oct 13 '16 at 23:50
  • Your edit doesn't seem to have gone through. – John Coleman Oct 14 '16 at 00:08
  • See [_Getting a Generator Object returned when I want the data_](http://stackoverflow.com/questions/40010322/getting-a-generator-object-returned-when-i-want-the-data) for some strong hints. You folks must all be working on the same homework... – martineau Oct 14 '16 at 01:36

1 Answers1

0

poly is a list, to index an element in this list, you need to supply a single index, not a pair of indices, which is why poly[x,y] is a syntax error. The elements are lists of length 2. If p is one of those elements, then for that point (x,y) = (p[0],p[1]). The following might give you an idea. enumerate allows you to simultaneously loop through the indices (needed to get the next point in the polygon) and the points:

>>> for i,p in enumerate(poly):
    print( str(i) + ": " + str((p[0],p[1])))


0: (31, 52)
1: (33, 56)
2: (39, 53)
3: (41, 53)
4: (42, 53)
5: (43, 52)
6: (44, 51)
7: (45, 51)
John Coleman
  • 51,337
  • 7
  • 54
  • 119