str_i + = e [0] +“\ t”+ e [1] +“\ n”

时间:2018-03-05 00:15:31

标签: python

我正在尝试打印堆栈,但我得到了这个:

str_i += e[0] + "\t" + e[1] + "\n"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

如何才能打印堆栈?

def __str__(self):
    str_i = 0
    for e in self.items:
        str_i += e[0] + "\t" + e[1] + "\n"
    return str_i       

s1 = Stack_2Queues()
s1.push(903, 12)
s1.push(767, 13)
s1.push(950, 14)
print(s1)

1 个答案:

答案 0 :(得分:0)

尝试使用:

str_i = ''
for e in self.items:
    str_i += "{}\t{}\n".format(e[0],e[1])