3 Replies - 914 Views - Last Post: 10 September 2012 - 06:33 AM Rate Topic: -----

#1 sonictmf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-June 12

Can't create linesegment object.

Posted 09 September 2012 - 09:20 PM

Hey everyone,

So I'm new to python, but i've programmed in java a bit. For class our teacher is having us create a line segment (purely textual, not visual). He has supplied us with the Point abstract data type, and we must use it to create a line segment abstract data type. Now I know that relying on everyone here to code this for me would be cheating, but I was wondering if you could just help me with my constructor, I can't seem to get it to work. Every time I try and create a 'linesegment' I get "name 'LineSegment' is not defined" Please help, below is a bit of my code.

import carpoint #This is where the point class is imported

class LineSegment :
    def __init__(self, pointA, pointB):
        self._firstPoint = pointA
        self._endPoint = pointB



So when I try:
lineseg1 = LineSegment(Point(3,3),Point(1,1))



It tells me that LineSegment is undefined.
Please help, I'm not sure what I'm doing wrong

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Can't create linesegment object.

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Can't create linesegment object.

Posted 10 September 2012 - 05:40 AM

I don't see anything wrong with your code that would give that error. Here's what you have and it works fine.
Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Can't create linesegment object.

Posted 10 September 2012 - 06:28 AM

Depends which module (py file) your code is in in relation to making the line segment.

If it's in the same py file it should work fine but otherwise you'll have to import it in one of two ways:

import mymodule
...
lineseg1 = mymodule.LineSegment(Point(3,3),Point(1,1))



from mymodule import LineSegment
...
lineseg1 = LineSegment(Point(3,3),Point(1,1))



Hope that helps.

This post has been edited by Simown: 10 September 2012 - 06:29 AM

Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • Joined: 21-June 11

Re: Can't create linesegment object.

Posted 10 September 2012 - 06:33 AM

I see that you imported the Point class qualified (i.e. you did import carpoint, not from carpoint import Point), but you used it unqualified (i.e. Point instead of carpoint.Point). This is wrong and will give you an error about Point not being defined. You need to either change your import statement or your usage of the Point class.

Of course the error message you're getting is about LineSegment, not about Point, so it's not caused by the issue above. But if you did the same thing with LineSegment that you did with Point, that is if your usage of LineSegment happens in another file than where you defined LineSegment, you used a qualified import to import it and then tried to use it without qualification, that would explain your error message.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1