题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
# 栈 # Push 将x加入栈,保证x为int整数 # pop 输出栈顶 并让栈定出栈 # top 输出栈顶 栈顶不出栈 ipt_num = int(input()) # 输入一个数字(input输入的都是字符串类型),强制转成整型 list_num = [] # 栈 for i in range(ipt_num): action = input() if action.split()[0] == "push": # 字符串分割 num = action.split()[1] list_num.append(num) elif action == "pop": if list_num: print(list_num.pop()) else: print("error") elif action == "top": if list_num: print(list_num[-1]) else: print("error")