3

I apologize in advance. I am a beginner at coding.

I am trying to use Python to code a function to calculate the area of a polygon using 2 lists that contain the node locations using the following code:

def Area(xvalues, yvalues):
    area = 0
    stepone = 0
    for x in xvalues:
        for y in yvalues:
            stepone = stepone + (xvalues(x)-xvalues(0))*(yvalues(y+1)-yvalues(y-1))
            area = abs(stepone)/2
    print area

xvalues = [2000, 2126, 2716, 2524, 2518, 2000]
yvalues = [1000, 1256, 1102, 408, 611, 1000]

Area(xvalues, yvalues)

However, I am getting an error that states "TypeError: 'list' object is not callable".

I just want the equation to cycle through the lists and return the final product. I am not sure where I am going wrong, but I think it may have something to do with my function parameters.

Any help would be greatly appreciated.

3
  • What exactly do you think the stepone = stepone + ... line will do? That's the problem line, you have parentheses where you should have brackets, but the x(0) is very confusing. Commented Mar 5, 2014 at 21:14
  • That line is suppose to read: stepone = stepone + (xvalues(x)-xalues(0))*(yvalues(y+1)-yvalues(y-1)) The subvalues following xvalues/yvalues is supposed to be corresponding to the position in the list. Commented Mar 5, 2014 at 21:22
  • in the future: look a little more carefully at the traceback, it will tell you exactly which line it has a problem with, and why. that usually gets you closer. Commented Mar 5, 2014 at 21:51

4 Answers 4

2

You're trying to call here: xvalues(x). If x were an index, you'd want xvalues[x] with square brackets instead of parentheses, but x is already the element itself! However, it looks like you're trying to access different elements than the ones you're getting out of the iterator. You can loop x in range(len(xvalues)), like this:

def Area(xvalues, yvalues):
    area = 0
    stepone = 0
    for x in range(len(xvalues)):
        for y in range(len(yvalues)):
            stepone = stepone + (xvalues[x]-x[0])*(yvalues[y+1]-yvalues[y-1])
            area = abs(stepone)/2
    print area

But there are a couple problems with it. First, yvalues[y-1] doesn't give you what you expect where y is 0. I don't know what you hope to make from that though. The best I can guess would be:

def Area(xvalues, yvalues):
    area = 0
    stepone = 0
    firstx = xvalues[0]
    for x in xvalues:
        for y1, y2 in zip(yvalues[1:], yvalues[:-1]):
            stepone = stepone + (x-firstx)*(y1-y2)
            area = abs(stepone)/2
    print area

The zip creates a new list, iterates over those, and assigns y1 and y2 to the elements of: [(1256, 1000), (1102, 1256), (408, 1102), (611, 408), (1000, 611)]

Sign up to request clarification or add additional context in comments.

3 Comments

You're comment has helped me make a lot of progress. I think I am close. I am trying to tell the code that if the index position is smaller than 0 or larger than 5 then nothing needs to be done. I added the if statement (if y < 0 or y > 6:), but the way i have it set up it is looking at the value in the list at that index position (ex: 1000, 1256, 1102, 408, 611, 1000) and not the actual index position itself (ex. 0,1,2,3,4,or5). Is there a way to look directly at the index position?
My first example is iterating over the index position. for x in range(len(xvalues))
YES!! for x in range(len(xvalues)-1) solved the problem. Thank you for all your help!
1

"TypeError: 'list' object is not callable" is thrown because you are trying to use a list like a function.

xvalues and yvalues are lists, so when you access them you need to use array notation. Round brackets are for typically functions, but lists are not functions and cannot be called like functions with round brackets. So it's just as the error says... "not callable".

Instead, use square brackets for indexing.

ie, the first value in xvalues can be found by calling xvalues[0], the third by xvalues[2] last by xvalues[-1]

To learn more of list manipulation read this page! :)

http://docs.python.org/2/tutorial/datastructures.html

When you write the line for x in xvalues I notice you are trying to use x like a list when you write x[0]. However x is an element of the xvalues list.. meaning that x is a integer, not a list.

You can imagine a that the loop-

for x in xvalues: ...

-is practically the same as:

for ii in range(0,len(xvalues)): x = xvalues[ii] ...

Hope this helps!

Comments

1

You are getting the error because you are trying to run the list as a function ('calling it'). The syntax to access elements from a list is as below:

>>myList = [1,2,3,4]
>>print mylist[1]
2

So you should use square brackets rather than braces in your formula. Taking a second look at your code, I realized that you are taking the absolute value to calculate your area within the loop. The fixed code would be of the following form. However, I am pressed on time so I cannot check the logic you are using to calculate stepone, so I am just leaving a comment there:

def Area(xvalues, yvalues):
    area = 0
    stepone = 0
    for x in xvalues:
        for y in yvalues:
            # logic to calculate stepone
    area = abs(stepone)/2
    return area

xvalues = [2000, 2126, 2716, 2524, 2518, 2000]
yvalues = [1000, 1256, 1102, 408, 611, 1000]

print Area(xvalues, yvalues)

Note that I also changed the function to return the value of the area rather than printing it. (This is just good practice.

1 Comment

Yeah I did not look hard enough at the math expression. I am going to weasel out of the geometry question though. :)
0

To solve your problem, you could simply do

x_values = [2000, 2126, 2716, 2524, 2518, 2000]
y_values = [1000, 1256, 1102, 408, 611, 1000]

area = 0
for x1, y1, x2, y2 in zip(x_values, y_values, x_values[1:], y_values[1:]):
    area += x1 * y2 - x2 * y1
area = abs(area) * 0.5

For these coords, this yields 272472.0. Ref.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.