我正在使用堆栈类为程序添加前缀。但是,每当我调用整数时,push() 方法都会引发 IndexError,即使我有一个异常处理程序并且我引用的是整数本身,而不是列表。
堆栈.py:
class stack():
def __init__(self,n):
self.n = n
self.top = -1
self.stack = [""] * n
#...
def push(self,c):
try:
print(self.top)
self.top += 1
self.stack[self.top] = c
except IndexError:
print("Stack is full.")
点子.py:
def toPrefix(input):
instack = stack(15)
prefix = ""
for i in range(0,len(input)):
for c in range(0,len(input[i])):
if(input[i][c].isalpha()):
instack.push(input[i][c])
错误:
Traceback (most recent call last):
File "<string>", line 247, in run_nodebug
File "P:\Scripts\Python\ascl-pip.py", line 42, in <module>
toPrefix(infix)
File "P:\Scripts\Python\ascl-pip.py", line 37, in toPrefix
instack.push(input[i][c])
File "P:\Scripts\Python\stack.py", line 36, in push
print(self.top)
IndexError: list assignment index out of range