python答疑:关于队列出栈和入栈的,报错是why?
classMyStack(object):def__init__(self,max):self.head=-1self.stack=list()self.max=maxf...
class MyStack(object):
def __init__(self, max):
self.head = -1
self.stack = list()
self.max = max
for i in range(self.max):
self.stack.append(0)
def put(self, item):
if self.head >= self.max:
return 'Put Error: The Stack is Overflow!'
else:
self.head += 1
self.stack[self.head] = item
print 'Put %s Success' % item
def get(self):
if self.head < 0:
return 'Get Error: The Stack is Empty!'
else:
self.head -= 1
return self.stack[self.head+1]
def isEmpty(self):
if self.head < -1:
return True
return False
if __name__ == "__main__":
mystack = MyStack(100)
mystack.put('a')
mystack.put('b')
print mystack.get()
mystack.put('c')
print mystack.get()
print mystack.get()
print mystack.get()
报错如下:
Traceback (most recent call last):
File "D:\python\ly\src\supercat\����ң.py", line 54, in <module>
mystack = MyStack(100)
TypeError: object.__new__() takes no parameters 展开
def __init__(self, max):
self.head = -1
self.stack = list()
self.max = max
for i in range(self.max):
self.stack.append(0)
def put(self, item):
if self.head >= self.max:
return 'Put Error: The Stack is Overflow!'
else:
self.head += 1
self.stack[self.head] = item
print 'Put %s Success' % item
def get(self):
if self.head < 0:
return 'Get Error: The Stack is Empty!'
else:
self.head -= 1
return self.stack[self.head+1]
def isEmpty(self):
if self.head < -1:
return True
return False
if __name__ == "__main__":
mystack = MyStack(100)
mystack.put('a')
mystack.put('b')
print mystack.get()
mystack.put('c')
print mystack.get()
print mystack.get()
print mystack.get()
报错如下:
Traceback (most recent call last):
File "D:\python\ly\src\supercat\����ң.py", line 54, in <module>
mystack = MyStack(100)
TypeError: object.__new__() takes no parameters 展开
2014-06-25
展开全部
没有问题啊 你的代码缩进有问题吧:
class MyStack(object):
def __init__(self, max):
self.head = -1
self.stack = list()
self.max = max
for i in range(self.max):
self.stack.append(0)
def put(self, item):
if self.head >= self.max:
return 'Put Error: The Stack is Overflow!'
else:
self.head += 1
self.stack[self.head] = item
print 'Put %s Success' % item
def get(self):
if self.head < 0:
return 'Get Error: The Stack is Empty!'
else:
self.head -= 1
return self.stack[self.head+1]
def isEmpty(self):
if self.head < -1:
return True
return False
if __name__ == "__main__":
mystack = MyStack(100)
mystack.put('a')
mystack.put('b')
print mystack.get()
mystack.put('c')
print mystack.get()
print mystack.get()
print mystack.get()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询