以下是一个简单的终端模拟程序的Python代码示例:

import os

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def print_prompt():
    print("Welcome to Terminal Simulator!")
    print("Enter 'help' for a list of commands.")
    print()

def print_help():
    print("Available commands:")
    print("help - Display this help")
    print("clear - Clear the screen")
    print("exit - Exit the program")
    print()

def run_terminal():
    clear_screen()
    print_prompt()

    while True:
        command = input("> ")

        if command == "help":
            print_help()
        elif command == "clear":
            clear_screen()
        elif command == "exit":
            break
        else:
            print(f"Unknown command: {command}")

    print("Goodbye!")

run_terminal()

这个程序会显示一个简单的命令行提示符,然后等待用户输入命令。支持的命令包括help(显示帮助信息)、clear(清屏)和exit(退出程序)。对于其他未知命令,会显示一条相应的错误消息。

注意:在不同的操作系统上,清屏命令可能不同。上面的代码使用了os.system函数来调用适用于Windows系统和其他操作系统的清屏命令。

Python 终端模拟程序:简单易懂的代码示例

原文地址: http://www.cveoy.top/t/topic/pyc1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录