Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stack python function debugging issue

I have implemented the stack in python code.

class stack:
    arrlen = 0
    def __init__(self,arr,poin):
        self.arr = arr
        self.poin = poin
        arrlen = len(self.arr)

    def push(obj):
        self.poin = (self.poin+1)%arrlen
        self.arr[self.poin] = obj

    def pop():
        self.poin = (self.poin-1)%arrlen

    def printStack():
        for i in range(self.poin):
            print("",self.arr[i])
            print("\n")

I am try to run this code:

x = int(input("Hello.Please write down the number of tasks you want to do for today:\n"))
array = [None]*x
y = 0
mytasks = stack(array,x);
while(y is not 4):
    y = int(input("What do you want to do 1)add task 2) remove task 3)print remaining tasks 4)exit\n"))
    if y is 1:
            tsk = input("Write down the name of your task:\n")
            mytasks.push(tsk)
    if y is 2:
            mytasks.pop()
    if y is 3:
            mytasks.printStack()

However every time I type 1 to add a task to the stack when the push()function is called it stops debugging and it shows me a error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: stack.push() takes 1 positional argument but 2 were given

What does this mean?

Before this issue I had had a issue with the init function so I had removed the arrlen variable but then I readded the arrlen variable so I dont know where I am wrong.I am using the IDLE Shell 3.10.4

>Solution :

self is the first argument of every python class method. Therefore, your method push should look like something like:

def push(self, obj):

    self.poin = (self.poin+1)%arrlen

    self.arr[self.poin] = obj

And even the methods where you don’t want to take any inputs, you should put self as the only parameter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading