048. 编写一个函数,实现简单的命令行接口,接受用户输入并响应
在 Python 中,可以通过 input()
函数创建一个简单的命令行接口,接受用户输入并根据输入内容进行响应。
示例代码
def simple_command_line_interface():"""实现一个简单的命令行接口,接受用户输入并响应。"""print("欢迎使用简单命令行接口!")print("输入 'help' 查看可用命令,输入 'exit' 退出程序。")while True:# 接受用户输入user_input = input("请输入命令:").strip()# 根据用户输入进行响应if user_input.lower() == 'exit':print("退出程序。")breakelif user_input.lower() == 'help':print("可用命令:")print(" help - 显示帮助信息")print(" exit - 退出程序")print(" greet <name> - 打印问候语")elif user_input.lower().startswith('greet'):# 解析用户输入的参数parts = user_input.split()if len(parts