Почему мой оператор if, выдающий объект TypeError: 'int', не вызывается? - PullRequest
0 голосов
/ 05 марта 2020

В исследованиях, которые я проводил, я видел много случаев, когда одно и то же слово используется в качестве имени переменной и имени функции или чего-то подобного, и, насколько я могу судить, я не есть что-нибудь из этого. Но у меня есть следующий код:

    operators = "['^', '*', '/', '+', '-']"

    def shuntParse(infix):
        print("shunting")
        output = ""
        operatorStack = Stack()

        for token in infix:
            print(token)
            try:
                float(token)
                output + ' ' + token
            except ValueError:
                if ((token in operators) and (operatorStack.capacity() > 0)):
                    while((operators.index(operatorStack.top()) > operators.index(token)) or (operators.index(operatorStack.top()) == operators.index(token))):
                        output + ' ' + operatorStack.pop()
                    operatorStack.push(token)
                elif ((token == '(') or (token in operators)):
                    operatorStack.push(token)
                elif (token == ')'):
                    while (operatorStack.top() != '('):
                        # If stack runs out without finding a left paren, parentheses are not matched
                        output + ' ' + operatorStack.pop()
                    operatorStack.pop()
                else:
                    pass

        while (operatorStack.capacity() != 0):
            output + ' ' + operatorStack.pop()

        return output

И когда он запускается, выдается следующая ошибка:

if ((token in operators) and (operatorStack.capacity() > 0)):
TypeError: 'int' object is not callable

Я действительно застрял на том, почему это происходит, и будет Люблю помощь. Спасибо!

Редактировать: Вот код, используемый для оператора Stack

class Stack:
    def __init__(self):
        self.head = None
        self.capacity = 0 

    def push(self, data):
        if (self.head == None):
            self.head = Node(data)                                                                                                                                                                          
            capacity += 1
        else:
            newNode = Node(data)
            newNode.nextNode(self.head)
            self.head = newNode
            capacity += 1

    def pop(self):
        if (self.head == None):
            pass
        else:
            newHead = self.head.nextNode
            oldData = self.head.data
            del self.head
            self.head = newHead
            capacity -= 1
            return oldData
    def top(self):
        return self.head.data
    def capacity(self):
        return self.capacity
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...