zoukankan      html  css  js  c++  java
  • 47-使用列表进行模拟栈

    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 your choice(0/1/2/3): """
    
        while True:
            # input()得到字符串,用strip()去除两端空白,再取下标为0的字符
            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()
  • 相关阅读:
    作业,注册页面
    HTML表单
    用HTML制作简历
    多线程同步标记
    集合类
    多态 接口
    java 泛型讲解
    转型
    使用super关键字
    作业题
  • 原文地址:https://www.cnblogs.com/hejianping/p/10907180.html
Copyright © 2011-2022 走看看