Python Forum
New beginner, defining point help :(
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New beginner, defining point help :(
#1
I'M USING PYTHON 3.2.1
================
So I have bought a book few days ago and got to it today. Its called "More Python Programming for the absolute beginner by Jonathan S. Harbour. I have copied the code to see how it works. On the way I experience few annoying errors I can't find a way to solve. Help would be nice  Big Grin

class Point():
    x = 0.0
    y = 0.0

    def _init_(self,x,y):
        self.x = x
        self.y = y
        print("Point constructor")

        def ToString(self):
            return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}"

    class Circle(Point):
        radius = 0.0

        def _init_(self,x,y,radius):
            super()._init_(x,y)
            self.radius = radius
            print("Circle constructor")

            def ToString(self):
                    return super().ToString() + \
                           ",{RADIUS=" + str(self.radius) + "}"

            p = Point(10,20)
            print(p.ToString())

            c = Circle(100,100,50)
            print(c.ToString())
ERROR:

line 1, in <module>

    class Point():

========================

line 13, in Point

    class Circle(Point):
Reply
#2
You're defining Circle within Point, as you can see from the indentation. Because of this, when you start trying to define Circle, Point has not yet been fully defined. Just dedent (unindent) the Circle class definition.
Reply
#3
Beside unindenting class definition you need to unindent both ToString methods, and your test code, so it would look like:
class Point():
   def __init_(self, x, y):
       ....
   def ToString(..
        ....
class Circle(Point):
   def __init__(...
       ...
   def ToString(..
       ....

p = Point ...
Morever you need to use __init__(double underscore) instead of _init_.
Reply
#4
NVM I FIXED IT :D THNX GUYS FOR THE HELP :DDD Seems really nothing for you guys, just another simple code but for me as a beginner I am so happy :)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,586 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020