《python核心编程第二版》练习题——实现堆栈操作(FILO)
2018-04-04 00:00
176 查看
#!/usr/bin/env python
stack = []
def pushit():
stack.append(input('Enter new string:').strip())
def popit():
if len(stack) == 0:
print ('Cannot pop from an empty stack!')
else:
print('Removed ', stack.pop())
def viewstack():
print(stack)
CMDs = {'u': pushit, 'o':popit, 'v':viewstack}
def showmenu():
pr = '''
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter your choice:'''
while True:
while True:
try:
choice = input(pr).strip()[0].lower()
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]()
if __name__ == '__main__':
showmenu()
stack = []
def pushit():
stack.append(input('Enter new string:').strip())
def popit():
if len(stack) == 0:
print ('Cannot pop from an empty stack!')
else:
print('Removed ', stack.pop())
def viewstack():
print(stack)
CMDs = {'u': pushit, 'o':popit, 'v':viewstack}
def showmenu():
pr = '''
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter your choice:'''
while True:
while True:
try:
choice = input(pr).strip()[0].lower()
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]()
if __name__ == '__main__':
showmenu()
相关文章推荐
- 用C语言堆栈操作的计算器的实现(输入输出流使用的C++)
- 堆栈的简单实现之一:基本操作(C语言实现)
- PIC 单片机 中的堆栈是硬件实现的,其深度已随芯片而固定,无法实现需要大量堆栈操作的递归算法
- 更新一些堆栈的操作实现
- 顺序堆栈的操作实现
- 《python核心编程第二版》练习题——实现1000以内的所有数字的英文表达
- 《python核心编程第二版》练习题——实现strip()函数
- 用类模板实现对任何类型的数据进行堆栈进行存取操作。
- 关于Java写逆波兰表达式堆栈操作简单实现
- 数据结构:请用一个数组实现两个堆栈,要求最大地利用数组空间,使 数组只要有空间入栈操作就可以成功
- 13数组实现顺序堆栈的基本操作
- 堆栈的基本操作(用数组实现)
- 堆栈链式实现入栈出栈等操作(C)
- 堆栈的基本操作(用链表实现)
- 多态实现线性表(队列,串,堆栈),要求具备线性表的基本操作
- openssl中的STACK操作实现了一个自动增加的堆栈
- 顺序表堆栈的操作实现源码
- 数组实现堆栈操作
- 试用多态实现线性表(队列,串,堆栈),要求具备线性表的基本操作:插入,删除,测长等。
- openssl中的STACK操作实现了一个自动增加的堆栈