I've taken up a challenge to recreate some popular Python functions, one of them being string.count(). Other than the substring argument and the start index (optional), it also takes the end index (optional as well). This is my code:
def count(self,substring,start=0):
self.substring = substring
self.start = start
self.queue = 0
self.counter = 0
for a in self.string[start:]:
if a == self.substring[0]:
self.queue += 1
if a == self.substring[len(self.substring) - 1] and self.queue != 0:
self.queue -= 1
self.counter += 1
return self.counter
Some variables I have defined outside of this function: self.string, which is the original string itself. You can see that I have not included the end argument, and that is why I'm here today. I would like to set the argument as: end=len(self.string) - 1, but it just throws and error and says: NameError: name 'self' is not defined. Is there a solution to this? So far I can say that my code is not the problem, since it works perfectly without the argument, but I may be wrong.