【发布时间】:2018-09-30 16:13:25
【问题描述】:
我尝试在 Python 中实现自己的堆栈。当我选择任何“pos”时,都会出现错误
class Stack:
'''This is Stack Class'''
def __init__(self):
self.stack=[]
def push(self):
'''Enter item to push into stack'''
self.stack.append(raw_input("Enter item to stack: "))
def pop(self):
'''Pops item from stack'''
if len(self.stack)==0:
print 'Cannot pop from empty stack'
else:
i = self.stack.pop(index=-1)
print ('Item popped: [%s]'%i)
def show(self):
'''Display Stack Content'''
print self.stack
choiceDict={'p':push, 'o':pop, 's':show, 'q':quit}
def menu():
'''This is a menu list for stack'''
s=Stack()
while True:
while True:
print '''Enter Choice
p) push
o) pop
s) show
q) quit'''
c=raw_input('Enter choice > ').lstrip()[0].lower()
if c not in 'posq':
print '**Invalid Choice'
else:
break
if(c=='q'):
break
s.choiceDict[c]()
if __name__=='__main__':
menu()
错误:
s.choiceDict[c]() TypeError: push() takes exactly 1 argument (0 given)
ps:如果代码中还有其他错误。我很高兴认识他们:)
顺便说一句,我只是想要解决这个问题的方法
【问题讨论】:
标签: python class oop dictionary python-2.x