Files

57 lines
1.5 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-12 课示例:函数
#
# 本文件演示函数的定义、调用、参数、返回值、默认参数和实际数据处理。
def print_separator():
"""输出分隔线,让命令行结果更容易阅读。"""
print("=" * 30)
def greet_user(name):
"""根据姓名输出问候语。"""
print(f"你好,{name}")
def build_agent_description(name, model="gpt-5"):
"""返回 Agent 的文字描述,而不是直接打印。"""
return f"Agent{name},模型:{model}"
def count_enabled_agents(agents):
"""统计列表中 enabled 为 True 的 Agent 数量。"""
enabled_count = 0
for agent in agents:
if agent["enabled"]:
enabled_count += 1
return enabled_count
def get_available_tools(agent):
"""返回 Agent 的工具列表;没有 tools 时返回空列表。"""
return agent.get("tools", [])
print_separator()
greet_user("Python 学习者")
print_separator()
print(build_agent_description("代码助手"))
print(build_agent_description("搜索助手", model="o3"))
print_separator()
agents = [
{"name": "代码助手", "enabled": True, "tools": ["搜索", "计算"]},
{"name": "搜索助手", "enabled": False, "tools": ["搜索"]},
{"name": "写作助手", "enabled": True, "tools": ["写作"]},
]
enabled_count = count_enabled_agents(agents)
print(f"启用的 Agent 数量:{enabled_count}")
for agent in agents:
tools = get_available_tools(agent)
print(f"{agent['name']} 的工具:{tools}")