步骤:
1.程序运行是不是交互的,主功能的需要的位置参数
2.菜单类,压栈,出栈,查询功能
3.先写框架,先后执行的顺序,写函数便于简化,主程序的运行
4.push pop view main框架完善,调入模块,定义变量
def push_it(): def pop_it(): def view_it(): def show_menu(): if __name__ == '__main__': show_menu()
改进
def push_it(): print('push') def pop_it(): print('pop') def view_it(): print('view') def show_menu(): prompt = """(0)push it (1)pop it (2)view it (3)exit Please input you choice(0/1/2/3):""" while True: choice = input(prompt).strip()[0] if choice not in '0123': print('Invalid input. Try again.') continue if choice == '3': break if choice == '0': push_it() elif choice == '1': pop_it() else: view_it() if __name__ == '__main__': show_menu()
#############################################################
stack = [] def push_it(): item = input('item to push :') stack.append(item) def pop_it(): if stack: print('from stack popped %s' % stack.pop()) def view_it(): print(stack) def show_menu(): cmds = {'0': push_it, '1': pop_it, '2': view_it} prompt = """(0)push it (1)pop it (2)view it (3)exit Please input you choice(0/1/2/3):""" while True: choice = input(prompt).strip()[0] if choice not in '0123': print('Invalid input. Try again.') continue if choice == '3': break cmds[choice]() if __name__ == '__main__': show_menu()