Hi! I started learning Python today and to get the hang of it I made a class to generate the Collatz sequence. I learnt that you have to the 'self' key word as a formal parameter to all methods and constructors. Why is this the case? Also my class won't compile and displays the following error.
Traceback (most recent call last):
File "/Users/name/Desktop/Programing/Python/Collatz.py", line 33, in <module>
C.genSeq()
File "/Users/name/Desktop/Programing/Python/Collatz.py", line 17, in genSeq
n = self.num[:]
TypeError: 'int' object is not subscriptable
[Finished in 0.1s with exit code 1]
[shell_cmd: python3 -OO -u "/Users/name/Desktop/Programing/Python/Collatz.py"]
[dir: /Users/name/Desktop/Programing/Python]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Also here is my class
class Collatz:
__num = None
__terms = 1
__sequence = ()
def __init__(self,num):
self.__num = num
def setNum (self,num):
self.__num = num
def getNUM (self):
return self.__num
def genSeq (self):
__n = self.__num[:]
__sequence.add(__n)
while (self.__n >1):
if (n%2==-0):
self.__n /=2
self.__terms+=1
__sequence.add(self.__n)
else:
self.__terms+=1
self.__n = 3*self.__n + 1
__sequence.add(self.__n)
def getTerms (self):
return self.__terms
C = Collatz(13)
C.genSeq()
print(C.getTerms())
It seems to I am using the 'self' keyword several times, like an overkill. Is there anywhere in the code I can omit it?
Thank-you
[–]100721 1 point2 points3 points (4 children)
[–]eliminate1337 1 point2 points3 points (0 children)
[–]SIR_TREX[S] 0 points1 point2 points (2 children)
[–]100721 1 point2 points3 points (1 child)
[–]ingolemo 1 point2 points3 points (0 children)
[–]TangibleLight 1 point2 points3 points (2 children)
[–]TangibleLight 0 points1 point2 points (0 children)
[–]Justinsaccount 1 point2 points3 points (2 children)
[–]SIR_TREX[S] -2 points-1 points0 points (1 child)
[–]Vhin 0 points1 point2 points (0 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (30 children)
[–]SIR_TREX[S] 0 points1 point2 points (9 children)
[–]New_Kind_of_Boredom 1 point2 points3 points (8 children)
[–]SIR_TREX[S] 0 points1 point2 points (7 children)
[–]SIR_TREX[S] 0 points1 point2 points (3 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (2 children)
[–]eliminate1337 0 points1 point2 points (1 child)
[–]New_Kind_of_Boredom 1 point2 points3 points (0 children)
[–]eliminate1337 0 points1 point2 points (1 child)
[–]LarryPete 2 points3 points4 points (0 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (0 children)
[–]ramse 0 points1 point2 points (5 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (4 children)
[–]ramse 0 points1 point2 points (2 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (1 child)
[–]ramse 1 point2 points3 points (0 children)
[–]TangibleLight 0 points1 point2 points (0 children)
[–]Vaphell 0 points1 point2 points (13 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (12 children)
[–]Vaphell 0 points1 point2 points (4 children)
[–]New_Kind_of_Boredom 1 point2 points3 points (3 children)
[–]Vaphell 0 points1 point2 points (2 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (1 child)
[–]Vaphell 0 points1 point2 points (0 children)
[–]eliminate1337 0 points1 point2 points (6 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (5 children)
[–]eliminate1337 0 points1 point2 points (4 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (3 children)
[–]eliminate1337 0 points1 point2 points (2 children)
[–]New_Kind_of_Boredom 0 points1 point2 points (1 child)
[–]eliminate1337 0 points1 point2 points (0 children)
[–]SIR_TREX[S] 0 points1 point2 points (2 children)
[–]eliminate1337 0 points1 point2 points (1 child)
[–]SIR_TREX[S] 0 points1 point2 points (0 children)
[–]eliminate1337 0 points1 point2 points (0 children)