|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 请问为啥这样不可以呢?不考虑为空的情况
- class Stack:
- def __init__(self,a=[]):
- self.a = []
- def isEmpty(self):
- if self.a:
- print("False")
- else:
- print('True')
- def show(self):
- for i in self.a:
- print(i,end=',')
- def Push(self,b):
- list(self.a).append(b)
- def Pop(self):
- print("顶部的数据是{}".format(self.a.pop()))
- def Top(self):
- print("顶部的数据是{}".format(self.a[-1]))
- def Bottom(self):
- print("底部的数据是{}".format(self.a[0]))
- >>> A = Stack('dadad')
- >>> A.Pop(
- 这是出错信息
-
- pop from empty list
复制代码
我把push方法那里的改成了self.a.append(b)
然后这样运行后是可行的
>>> A = Stack()
>>> A.Push("dadda")
>>> A.a
['dadda']
>>> A.Pop()
顶部的数据是dadda
|
|