Posts: 7
Threads: 1
Joined: Feb 2019
Feb-20-2019, 06:10 AM
(This post was last modified: Feb-20-2019, 07:21 AM by buran.)
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
Posts: 8,158
Threads: 160
Joined: Sep 2016
Feb-20-2019, 07:59 AM
(This post was last modified: Feb-20-2019, 07:59 AM by buran.)
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.
Posts: 7
Threads: 1
Joined: Feb 2019
nope its from a book , for practice with classes
Posts: 8,158
Threads: 160
Joined: Sep 2016
Feb-20-2019, 08:49 AM
(This post was last modified: Feb-20-2019, 09:01 AM by buran.)
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
Posts: 7
Threads: 1
Joined: Feb 2019
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
Posts: 8,158
Threads: 160
Joined: Sep 2016
Feb-20-2019, 11:16 AM
(This post was last modified: Feb-20-2019, 11:16 AM by buran.)
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? :-)
Posts: 7
Threads: 1
Joined: Feb 2019
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","!!!")
Posts: 8,158
Threads: 160
Joined: Sep 2016
Feb-20-2019, 11:34 AM
(This post was last modified: Feb-20-2019, 11:34 AM by buran.)
Yes, there are 2 problems in the SuperClass as I said
it's in the __init__() method
Posts: 7
Threads: 1
Joined: Feb 2019
Feb-20-2019, 11:55 AM
(This post was last modified: Feb-20-2019, 11:55 AM by thomaskissas40.)
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
Posts: 8,158
Threads: 160
Joined: Sep 2016
Feb-20-2019, 11:57 AM
(This post was last modified: Feb-20-2019, 11:57 AM by buran.)
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()
|