题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
import sys def push(stack: list, x): stack.append(x) return stack def pop(stack: list): if(stack): print(stack.pop()) else: print("error") return stack def top(stack: list): if(stack): print(stack[-1]) else: print("error") return stack if __name__ == "__main__": stack = [] for line in sys.stdin: if line.startswith("push "): stack = push(stack, int(line.replace("push ", ""))) elif line.startswith("pop"): stack = pop(stack) elif line.startswith("top"): stack = top(stack)