[python-3] TypeError:必须是str,而不是int

时间:2017-10-19 16:11:22

标签: python python-3.x parsing binary-tree typeerror

我的 Parse Tree 代码存在问题。当我尝试使用后订单显示它时,它会给出一条错误消息,指出应该是str参数而不是int

from classStack import Stack
from classTree import BinaryTree

def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    pStack.push(eTree)
    currentTree = eTree
    for i in fplist:
        if i == '(':
            currentTree.insertLeft('')
            pStack.push(currentTree)
            currentTree = currentTree.getLeftChild()

        elif i not in ['+', '-', '*', '/', ')']:
            currentTree.setRootVal(int(i))
            parent = pStack.pop()
            currentTree = parent

        elif i in ['+', '-', '*', '/']:
            currentTree.setRootVal(i)
            currentTree.insertRight('')
            pStack.push(currentTree)
            currentTree = currentTree.getRightChild()

        elif i == ')':
            currentTree = pStack.pop()

        else:
            raise ValueError("No se contempla el carácter evaluado")

    return eTree

ParseTree = buildParseTree("( 8 * 5 )")
ParseTree.printPostordenTree(0)

错误:

Traceback (most recent call last):
  File "C:\Users\Franco Calvacho\Downloads\Árboles\parseTree.py", line 38, in <module>
    ParseTree.printPostordenTree(0)
  File "C:\Users\Franco Calvacho\Downloads\Árboles\classTree.py", line 62, in printPostordenTree
    self.getLeftChild().printPostordenTree(n+1)
  File "C:\Users\Franco Calvacho\Downloads\Árboles\classTree.py", line 67, in printPostordenTree
    print("Level: "+ str(n) + " " + self.getRootVal())
TypeError: must be str, not int
[Finished in 0.2s]

这是BinaryTree代码。我明确表示只有BinaryTree代码是可以的,它并没有给我这个错误信息。 根据该代码,我将留下您的python评论,供您查看

class BinaryTree(object):
    def __init__(self, data): 
        self.key = data
        self.leftChild = None 
        self.rightChild = None 

    def insertLeft(self, newNode): 
        if self.leftChild == None:  
            self.leftChild = BinaryTree(newNode) 
        else: 
            t = BinaryTree(newNode) 
            t.leftChild = self.leftChild 
            self.leftChild = t 

    def insertRight(self, newNode): 
        if self.rightChild == None:
            self.rightChild = BinaryTree(newNode)
        else: 
            t = BinaryTree(newNode)
            t.rightChild = self.rightChild
            self.rightChild = t 

    def getRightChild(self):
        return self.rightChild

    def getLeftChild(self):
        return self.leftChild

    def setRootVal(self, obj): 
        self.key = obj

    def getRootVal(self):
        return self.key     

    def printPreordenTree(self, n): 
        if self.getRootVal() != None: 
            print("Level: "+ str(n) + " " + self.getRootVal())
            n+=1 

            if self.getLeftChild() != None:
                self.getLeftChild().printPreordenTree(n) 

            if self.getRightChild() != None:
                self.getRightChild().printPreordenTree(n) 
        return n 

    def printInordenTree(self, n): 
        if self.getRootVal() != None: 
            if self.getLeftChild() != None: 
                self.getLeftChild().printInordenTree(n+1) 

            print("Level: "+ str(n) + " " + self.getRootVal())
            n+=1 

            if self.getRightChild() != None: 
                self.getRightChild().printInordenTree(n) 
        return n 

    def printPostordenTree(self, n):
        if self.getRootVal() != None:
            if self.getLeftChild() != None:
                self.getLeftChild().printPostordenTree(n+1)

            if self.getRightChild() != None:
                self.getRightChild().printPostordenTree(n+1) 

            print("Level: "+ str(n) + " " + self.getRootVal())
            n+=1 
        return n 

"""a = BinaryTree("a")
a.insertLeft("b") 
a.insertRight("c") 
a.getLeftChild().insertLeft("d") 
a.getRightChild().insertLeft("e") 
a.getRightChild().insertRight("f") 
print("Imprimo el árbol de forma Preorden")
a.printPreordenTree(0)
print("\nImprimo el árbol de forma Inorden")
a.printInordenTree(0)
print("\nImprimo el árbol de forma Postorden")
a.printPostordenTree(0)"""

1 个答案:

答案 0 :(得分:5)

print("Level: "+ str(n) + " " + self.getRootVal())
TypeError: must be str, not int

这解释了错误:您只能使用&#34; +&#34;将字符串附加到字符串。运算符,而不是整数。 前三个参数是字符串,但self.getRootVal()不是。

第一种可能的解决方案:将其投射到str,就像使用n

一样
print("Level: "+ str(n) + " " + str(self.getRootVal()))

或者您可以使用格式字符串:

print("Level: {} {}".format(n, self.getRootVal()))

后者不要求您将参数强制转换为字符串,并且可能更易读。