er232112 发表于 2015-7-16 08:56:55

python 列表模拟堆栰

#!/usr/bin/env python
stack = []


def pushit():
    stack.append(raw_input('Enter New string:').strip())


def popit():
    if len(stack) == 0:
      print "Cannot pop from an empty stack!"
    else:
      r=stack.pop()
      print "Removed [ %s]" %r

def viewstatck():
    print stack

CMDs = {'u':pushit,'o':popit,'v':viewstatck}

def showmenu():
    pr="""
    p(U)sh
    P(O)p
    (V)iew
    (Q)uit
    Enter choice:"""

    while True:
      choice = raw_input(pr).strip().lower()
      print "\nYou picked:[%s]"%choice
      if choice == 'q':
            break
      if choice not in 'uovq':
            print 'Invalid option,tyr again'
            continue
      else:
            CMDs()

if __name__ == '__main__':
    showmenu()


页: [1]
查看完整版本: python 列表模拟堆栰