1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
import sys import tty import termios import atexit
try: while True: print("请输入 '1' 打印1, '2' 打印2, 或输入 'q' 退出程序:") fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) if ch == '1': print("1") elif ch == '2': print("2") elif ch.lower() == 'q': print("程序即将退出...") break else: print("无效的输入,请输入 '1', '2', 或 'q'。") except KeyboardInterrupt: print("\n程序因键盘中断而退出。") finally: print("退出")
|