Files

102 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 第 1-7 课课堂练习Agent 提示词文本分析器
#
# 完成要求:
# 1. 根据注释使用字符串索引、切片和常用方法。
# 2. 判断英文关键词时忽略大小写。
# 3. 使用有意义的英文蛇形变量名。
# 4. 不要删除题目注释。
# 5. 完成后使用题目提供的文本测试结果。
# 练习一:使用 input() 接收一段 Agent 提示词。
# 将原始输入保存到 raw_prompt。
# 练习二:使用 strip() 清理提示词两侧空白。
# 将结果保存到 clean_prompt。
# 练习三:使用 len() 统计清理后的字符数量。
# 将结果保存到 prompt_length。
# 练习四:创建用于忽略大小写判断的字符串。
# 对 clean_prompt 调用 lower(),将结果保存到 lower_prompt。
# 练习五:使用 in 完成两个关键词判断:
# 1. lower_prompt 是否包含 "python",结果保存到 contains_python
# 2. lower_prompt 是否包含 "agent",结果保存到 contains_agent。
# 练习六:使用 count() 统计 lower_prompt 中 "agent" 出现的次数。
# 将结果保存到 agent_count。
# 练习七:使用 split() 按照空白拆分 clean_prompt。
# 不需要给 split() 传入参数。
# 将结果保存到 word_list再使用 len() 统计分割后的数量,保存到 word_count。
# 提示:中文句子通常不会按每个汉字拆分;本练习统计的是空白分隔后的文本片段。
# 练习八:使用索引和切片取得摘要信息。
# 先使用 if 判断 prompt_length 是否大于 0避免空字符串索引越界
# - 非空时first_character 保存第一个字符last_character 保存最后一个字符;
# - 空字符串时:两个变量都保存 "无"。
# 再使用切片 clean_prompt[:10] 取得最多前 10 个字符,保存到 preview_text。
# 练习九:使用 replace() 将 clean_prompt 中的 "帮我" 替换为 "协助我"。
# 将结果保存到 improved_prompt。
# 练习十:创建字符串 "Python,FastAPI,Django"。
# 使用 split(",") 将它拆分并保存到 technology_list。
# 再使用 " -> ".join(technology_list) 连接,保存到 technology_path。
# 练习十一:接收一个 Python 文件名并清理两侧空白。
# 使用 endswith(".py") 判断是否为小写 .py 结尾,结果保存到 is_python_file。
# 练习十二:使用 30 个等号输出分隔线,再使用 f-string 输出分析报告:
# 1. 清理后的提示词;
# 2. 字符数量 prompt_length
# 3. 文本片段数量 word_count
# 4. 第一个字符和最后一个字符;
# 5. 前 10 个字符 preview_text
# 6. 是否包含 Python
# 7. 是否包含 Agent
# 8. Agent 出现次数;
# 9. 替换后的提示词 improved_prompt
# 10. 技术路线 technology_path
# 11. 文件名是否以 .py 结尾。
# 练习十三:根据分析结果输出一条建议:
# - clean_prompt 为空:输出“提示词不能为空。”
# - 不包含 Agent输出“建议明确说明 Agent 相关任务。”
# - 不包含 Python输出“建议补充需要使用的编程语言。”
# - 同时包含 Agent 和 Python输出“提示词包含核心技术信息。”
# 请使用 if...elif...else并注意判断顺序。
# 第一次测试建议输入:
# 提示词: 请使用 Python 帮我开发一个 Agent 应用
# 文件名agent_service.py
#
# 预期核心结果:
# contains_python = True
# contains_agent = True
# agent_count = 1
# is_python_file = True
# 最终建议为“提示词包含核心技术信息。”
# 第二次测试建议输入:
# 提示词:请帮我整理学习计划
# 文件名notes.txt
#
# 预期核心结果:
# contains_python = False
# contains_agent = False
# is_python_file = False
# 最终建议优先提示缺少 Agent 相关任务。
# 完成后进行自查:
# 1. 是否先 strip() 再进行长度和关键词分析;
# 2. 是否使用 lower_prompt 进行英文关键词判断;
# 3. 空字符串是否避免了索引越界;
# 4. 切片是否最多取得前 10 个字符;
# 5. split() 与 join() 的方向是否正确;
# 6. 最终建议的判断顺序是否正确。