我为一个项目创建了以下stack
类,但是无法让它正常运行。我无法判断我是否犯了错误,或者我的TA给出的主要功能是错误,无论如何这里是我的代码:
class Stack:
#takes in the object as input argument
#will not return anything
def __init__(self):
#initialise an instance variable to an empty list.
self.items=[]
#takes in the object as input argument
#return value Type: True or False
def isEmpty(self):
#check if the list is empty or not. If empty, return True else return False
if self.items == []:
return True
else:
return False
#takes in the object as the first argument
#takes the element to be inserted into the list as the second argument
#should not return anything
def push(self, x):
#add the element to be inserted at the end of the list
self.items.append(x)
#takes in the object as the input argument
#if the list is not empty then returns the last element deleted from the list. If the list is empty, don't return anything
def pop(self):
#check if the list is Empty
#if Empty: print the list is empty
#if the list is not empty, then remove the last element from the list and return it
if self.isEmpty()==True:
print("the list is empty")
else:
return self.items.pop()
#takes in the object as the input argument
#should not return anything
def printContents(self):
#if the list is not empty, then print each element of the list
print("The content of the list is", self.items)
根据评论,任何人都可以给我任何关于如何使这项工作更合适的建议吗?对不起,我不是计算机科学家,我正在努力理解我的python类的类和函数。
from stack import *
def main():
s = Stack()
s.push(1)
s.pop()
s.pop()
s.push(2)
s.push(3)
s.push(4)
s.printContents()
main()
答案 0 :(得分:0)
将类中的pop定义修改为此
def pop(self):
#check if items is empty
if len(self.items)==0:
#if empty
return "Nothing to pop"
else:
#if not empty
return str(self.items.pop())+" Popped"
答案 1 :(得分:0)
你应该好好看看空间和对齐方式。例如,printContents
未正确对齐。请注意,正确的对齐在python中非常非常重要。
此外,您不能在printContents
打印。这应该有效:
class Stack:
#takes in the object as input argument
#will not return anything
def __init__(self):
#initialise an instance variable to an empty list.
self.items=[]
#takes in the object as input argument
#return value Type: True or False
def isEmpty(self):
#check if the list is empty or not. If empty, return True else return False
if self.items == []:
return True
else:
return False
#takes in the object as the first argument
#takes the element to be inserted into the list as the second argument
#should not return anything
def push(self, x):
#add the element to be inserted at the end of the list
self.items.append(x)
#takes in the object as the input argument
#if the list is not empty then returns the last element deleted from the list. If the list is empty, don't return anything
def pop(self):
#check if the list is Empty
#if Empty: print the list is empty
#if the list is not empty, then remove the last element from the list and return it
if self.isEmpty():
print("the list is empty")
else:
return self.items.pop()
#takes in the object as the input argument
#should not return anything
def printContents(self):
#if the list is not empty, then print each element of the list
print("the contents of the list are", self.items)
def main():
s = Stack()
s.push(1)
s.pop()
s.pop()
s.push(2)
s.push(3)
s.push(4)
s.printContents()
main()
你可以在这里看到它在线工作:
答案 2 :(得分:0)
每个人都必须在某个时间开始,所以不要担心,我希望你不会被我的扩展批评所冒犯,因为我只是想帮助你。
您可能希望顶级声明为:
class Stack(object):
离开(对象):部分是一个不推荐使用的表单,它使得该类的某些功能在您可能不想要的方式上有所不同。
其次,声明一个isEmpty方法并不是一个普通的" pythonic"做法。强烈期望的python约定是简单地检查对象的真值,空为False。您的类的典型python用户不会期望isEmpty方法。您可以通过定义特殊的非零来控制对象的真值的行为,您可以这样写:
def __nonzero__(self):
return bool(self.items)
这样有人可以使用你的课程:
stack = Stack()
stack.append("apple")
stack.append("pear")
if stack:
print "stack has stuff in it"
else:
print "stack is empty"
此外,还有其他一些事情你应该重载以创建一个好的堆栈类。例如,你应该支持len(堆栈),你应该能够迭代你的堆栈。您可以定义特殊的 len 和 iter 方法来执行此操作。请注意,如果您定义 len ,那么您不需要定义 iszero (如果 len 返回0,python会认为您的堆栈为False并且您尚未定义 iszero )。
如果没有print或write语句,printContents方法将不会打印或写入任何内容,但如果堆栈为空,则会返回堆栈中的最后一个元素并抛出索引错误。要迭代每个项目并打印它,您可以像这样写(从堆栈的顶部项目到第一个项目):
def printContents(self):
for item in reversed(self.items):
print item
然而,定义迭代方法及其用途会更加pythonic,因此您的用户可以迭代堆栈并自行打印:
def __iter__(self):
for item in self.items:
yield item
# later in code .....
stack = Stack()
stack.append("apple")
stack.append("pear")
for item in stack:
print item
希望这些提示可能有用。坚持下去,你很快就会得到它。