6 Replies - 1813 Views - Last Post: 06 September 2013 - 01:00 PM Rate Topic: -----

#1 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Undefined variable error with a method

Posted 05 September 2013 - 02:24 PM

First project in Python. The implicit concept is throwing me off and I think that might be the problem. The error is in line 35 of the module Main. Ill also post the line and point modules. The assignment is supposed to see if lines intersect within a certain range. Any help on this error? Been stuck for awhile.

'''
Created on Sep 4, 2013

@author: James
'''
import Point
import LineSegment
from sre_constants import RANGE


''' test method '''


arr = []
for i in range(0, 99):
    x = []
    for j in range(0, 2):
        x.append(0)
        arr.append(x)

for i in range(0,99):  
    '''Fill with random lines'''
    point1 = Point.Point(0,0)
    point1.random()
    point2 = Point.Point(0,0)
    point2.random()
    point3 = Point.Point(0,0)
    point3.random()
    point4 = Point.Point(0,0)
    point4.random()
    arr[i][0] = LineSegment.MyClass(point1,point2)
    arr[i][1] = LineSegment.MyClass(point3,point4)
    print(" Line 1  [ " + str(point1.getX()) + "," + str(point1.getY()) + " ]" + " [ " + str(point2.getX()) + "," + str(point2.getY()) + " ]")
    print(" Line 2  [ " + str(point3.getX()) + "," + str(point3.getY()) + " ]" + " [ " + str(point4.getX()) + "," + str(point4.getY()) + " ]")
    intersect = LinesIntersect(arr[i][0], arr[i][1])
    print(" Intersect: " + intersect );
    
    
def LinesIntersect ( line1, line2):
    
    '''First determine if Parrallel'''
    if (line1.getSlope() == line2.getSlope()):
        return False
    else:
        ''' Lines are not parallel, check if point of intersection is in range
        First find the  y intercepts'''
        intercept1 = line1.getA().getY() - line1.getSlope * line1.getA.getX()
        intercept2 = line2.getA().getY() - line2.getSlope * line2.getA.getX()
        ''' Next find the intersects'''
        intersectX = (intercept1/intercept2) / ( line1.getSlope - line2.getSlope)
        intersectY = line1.getSlope * intersectX + intercept1
        if ( intersectX > line1.getA().getX().MAX_X 
             or intersectX < 0 
             or intersectY > line1.getA().getY().MAX_Y 
             or intersectY < 0):
            return False
        else:
            return True
            
    


'''
Created on Sep 4, 2013

@author: James
'''

''' A line segment should consists of two points: A and B '''
class MyClass(object):
    
    
    '''Constructor Takes 2 points as an argument'''
    def __init__(self, pointA, pointB):
        self.A = pointA
        self.B = pointB
    
    ''' Returns point A'''
    def getA(self):
        return self.pointA
    ''' Returns point B'''
    def getB(self):
        return self.pointB
    ''' Sets point A as the point passed through the argument'''
    def setA(self, point):
        self.A = point
    ''' Sets point B as the point passed through an argument'''
    def setB(self, point):
        self.B = point
    ''' Returns the slope of the line'''
    def getSlope(self):
        slope = (self.getA().getY() - self.getB().getY())/(self.getA().getX() - self.getB().getX())
        return slope
       
        

This post has been edited by andrewsw: 05 September 2013 - 04:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined variable error with a method

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Undefined variable error with a method

Posted 05 September 2013 - 02:40 PM

Which is the module Main and what is line 35 in your posted code?

Post the full error message as well.
Was This Post Helpful? 0
  • +
  • -

#3 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Re: Undefined variable error with a method

Posted 05 September 2013 - 03:29 PM

View Postandrewsw, on 05 September 2013 - 02:40 PM, said:

Which is the module Main and what is line 35 in your posted code?

Post the full error message as well.


It is the first module. The line is
intersect = LinesIntersect( arr[i][0], arr[i][1])

The error message is
NameError: name 'LinesIntersect' is not defined
Was This Post Helpful? 0
  • +
  • -

#4 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Re: Undefined variable error with a method

Posted 05 September 2013 - 03:58 PM

So I'm pretty sure it has to do with the linesIntersect method's scope. I'm confused on if I should have a main method and if this method can be static.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Undefined variable error with a method

Posted 05 September 2013 - 04:20 PM

Yes, in Python the declaration order is significant, and using a main method is a common approach to resolve this:
def main():
    # your main code runs from here..
    print(Foo())

def Foo():
    return "Ham and Eggs"    

if __name__ == '__main__':
    main()

The call to main() occurs after the function definitions have been executed (parsed).

PS I removed your second code section (from the OP) as it was a repeat of the first.

This post has been edited by andrewsw: 05 September 2013 - 04:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 261
  • Joined: 11-May 13

Re: Undefined variable error with a method

Posted 05 September 2013 - 05:16 PM

'Noob' Python First Programs are getting more and more complex! :wheelchair:
Was This Post Helpful? 0
  • +
  • -

#7 Hikaroshi  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 42
  • Joined: 26-April 09

Re: Undefined variable error with a method

Posted 06 September 2013 - 01:00 PM

To add on to that explanation above, with the code you have posted, your code runs line by line. Once it reaches that function call, it doesn't recognize the function because it did not reach that line where the function is created, which happens after you try to call it. Like this link. If you step through it, you realize what is going on. It would technically work if the call happened after the program had reached that line and created a pointer to location of the function definition, like in this example.

If you have everything in a function, the program will run line by line, but in a different sense. It will go from top to bottom and point to where each function is defined, but will only execute (explore the contents of the function) the function if it is called. In the case of this last example if newFunction() was not called on the last line of the program, the program would just contain pointers to the functions but would not execute anything because none of the functions were called. Then from there, newFunction() calls the other function on line 7.

This post has been edited by Hikaroshi: 06 September 2013 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1