stack = [] # 初始化一个列表数据类型对象, 作为一个栈
def pushit(): # 定义一个入栈方法
stack.append(raw_input('Enter New String: ').strip())
# 提示输入一个入栈的 String 对象, 调用 Str.strip() 保证输入的 String 值不包含多余的空格
def popit(): # 定义一个出栈方法
if len(stack) == 0:
print "Cannot pop from an empty stack!"
else:
print 'Remove [', `stack.pop()`, ']'
# 使用反单引号(` `)来代替 repr(), 把 String 的值用引号扩起来, 而不仅显示 String 的值
def viewstack(): # 定义一个显示堆栈中的内容的方法
print stack
CMDs = {'u':pushit, 'o':popit, 'v':viewstack}
# 定义一个 Dict 类型对象, 将字符映射到相应的 function .可以通过输入字符来执行相应的操作
def showmenu(): # 定义一个操作菜单提示方法
pr = """
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: """
while True:
while True:
try:
choice = raw_input(pr).strip()[0].lower()
# Str.strip() 去除 String 对象前后的多余空格
# Str.lower() 将多有输入转化为小写, 便于后期的统一判断
# 输入 ^D(EOF, 产生一个 EOFError 异常)
# 输入 ^C(中断退出, 产生一个 keyboardInterrupt 异常)
except (EOFError, KeyboardInterrupt, IndexError):
choice = 'q'
print '\nYou picked: [%s]' % choice
if choice not in 'uovq':
print 'Invalid option, try again'
else:
break
if choice == 'q':
break
CMDs[choice]()
# 获取 Dict 中字符对应的 functionName, 实现函数调用
if __name__ == '__main__':
showmenu()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有