首页 /编程语言和算法/Python
 Python 的清屏命令,调用清屏函数,清除控制台屏幕 子程序 clear_screen()
2025年4月22日 01:15
# 导入 os 模块
import os

try:
    print('Hello')

    # 定义清屏函数,根据操作系统类型执行不同的清屏命令
    def clear_screen():
        # 如果操作系统是 Windows,执行 'cls' 命令清屏;否则执行 'clear' 命令
        os.system('cls' if os.name == 'nt' else 'clear')

    # 调用清屏函数,清除控制台屏幕
    clear_screen()

    print('World')
except Exception as e:
    print(f"程序执行过程中出现错误: {e}")
finally:
    input("Press Enter to exit...")


 
全部回复(0)
首页 | 电脑版 |