菜单类,压栈,出栈,查询功能

步骤:

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值