Python Forum

Full Version: New beginner, defining point help :(
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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):
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.
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_.
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 :)))