Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Help
#1
Dear Friends
I am new in Python Programming and i try to exercise myself with some small programs. I try to study classes so i made the above small program.

class SuperClass:
    def _init_(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
        def getAttribute1(self):
            return self.attritube1
        def setAttribute2(self,a1):
            self.attritube1=a1
        def getAttribute2(self):
            return self.attribute2
        def setAttribute1(self,a2):
            self.attribute2=a2

class SubClass (SuperClass):
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    
    testObject=SubClass("Hello","World","!!!")
    textObject.printMessage()
   
So my result has to print > My attributes are: Hello world!!
I have some mistakes but i cant find them
Reply
#2
We are long time here and we have seen this exercise. We know this code is from your professor and your task is to find the errors and make small changes so that it works. So please, at least try to fix them yourself first and come with something.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
nope its from a book , for practice with classes
Reply
#4
it's word for word same as your classmate asked :-) and they were honest enough to tell us it's an university assignment.
Even if it's from a book - what you have tried?
(Feb-20-2019, 06:10 AM)thomaskissas40 Wrote: I try to study classes so i made the above small program.
(Feb-20-2019, 06:10 AM)thomaskissas40 Wrote: I have some mistakes but i cant find them

At least don't claim you have made the program
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Yes i tried something like this:
class SubClass (SuperClass):
    def __init__(self,a3):
        super().__init__(self,a3)
        self.attribute3=a3
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    
    testObject=SubClass("Hello","World","!!!")
    testobject.printMessage()

input
NameError: name 'SubClass' is not defined
Reply
#6
OK, now that's fair. The problem is the indentation of the last 2 lines - they are part of SubClass. Dedent them.
And what was the problem with the SuperClass? :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I try this but i still have problems
class SuperClass:
    def _init_(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
        def getAttribute1(self):
            return self.attritube1
        def setAttribute2(self,a1):
            self.attritube1=a1
        def getAttribute2(self):
            return self.attribute2
        def setAttribute1(self,a2):
            self.attribute2=a2
class SubClass (SuperClass):
    def __init__(self,a3):
        super().__init__(self,a3)
        
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    testObject=SuperClass()
    testObject=SubClass("Hello","World","!!!")
    testΟbject.printMessage()
Traceback (most recent call last):
File "/home/main.py", line 20, in <module>
class SubClass (SuperClass):
File "/home/main.py", line 31, in SubClass
testObject=SubClass("Hello","World","!!!")
Reply
#8
Yes, there are 2 problems in the SuperClass as I said
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
class SuperClass:
    def __init__(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
        def getAttribute1(self):
            return self.attritube1
        def setAttribute2(self,a1):
            self.attritube1=a1
        def getAttribute2(self):
            return self.attribute2
        def setAttribute1(self,a2):
            self.attribute2=a2
class SubClass (SuperClass):
    def __init__(self,a3):
        SuperClass.__init__(self,a1,a2)
        
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    testObject=SuperClass()
    testObject=SubClass("Hello","World","!!!")
    testΟbject.printMessage()
Well i guess i cant find the problem
Reply
#10
Look at the undescores on each side of init. Do they look ok?
Is the indentation of the methods in SuperClass correct?

And you don't need testObject=SuperClass()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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