|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下为程序执行结果:
>>> stack = Stack(5)
>>> stack.push()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
stack.push()
File "J:/python/python exercise/39 lesson/stack class.py", line 7, in push
list1.append(self.x)
NameError: name 'list1' is not defined
>>>
以下为源程序:
class Stack:
list1 = []
def __init__(self, x):
self.x = x
def push(self):
list1.append(self.x)
def isEmpty(self):
if Stack.list1:
return True
else:
return False
def pop(self):
list1.pop(self.x)
def top(self):
return list1[-1]
def bottom(self):
return list1[0]
- class Stack:
- list1 = []
- def __init__(self, x):
- self.x = x
-
- def push(self):
- self.list1.append(self.x)
- def isEmpty(self):
- if Stack.list1:
- return True
- else:
- return False
- def pop(self):
- list1.pop(self.x)
- def top(self):
- return list1[-1]
- def bottom(self):
- return list1[0]
复制代码
|
|