实验 04 · Codex + Python
Codex 与 Python 小项目
用熟悉的 Python 列表计算平均值、平方、平方和和中心化值。
核心问题
如何让 Codex 帮助写 Python,同时自己检查数学结果?
复用技能
- 运行 Python 脚本
- 写 Codex 提示词
- 解释平均值和中心化值
主要任务
- 复制并运行 Lab 4 starter。
- 让 Codex 创建 numbers_summary.py。
- 手算检查平均值、平方和中心化值。
提交内容
- labs/lab04/numbers_summary.py
- notes/lab04-notes.md
- 终端输出截图
完整讲义
实验 4:Codex 与 Python 小项目
目标
在一个小型 Python 项目中使用 Codex,同时继续复用 PowerShell、VS Code 和实验笔记工作流。
本实验为实验 5 的 Python 阶段性小作业做准备。请认真记录命令、文件结构和人工检查, 因为下一次实验会复用这些工作流。
数学任务
对列表:
[1, 2, 3, 4, 5]
计算:
- 平均值;
- 每个数的平方;
- 平方和;
- 中心化后的值,即每个值减去平均值。
设置
- 打开课程根目录:
``powershell cd Desktop\codex-matlab-class mkdir labs\lab04 code . ``
- 将
starters/lab04/中的 starter 文件复制到labs/lab04/。 - 创建
notes/lab04-notes.md。 - 运行 starter 文件:
``powershell python labs\lab04\hello.py python labs\lab04\average.py ``
Codex 任务
从课程根目录启动 Codex:
codex
提示词 1:
I am a math student with basic Python knowledge.
In labs/lab04, create a file named numbers_summary.py.
It should define [1, 2, 3, 4, 5], compute the average, squares, sum of squares,
and centered values. Keep the code beginner-friendly and add comments.
After editing, explain how I should run it from PowerShell.
提示词 2:
Please explain numbers_summary.py line by line.
Focus on the mathematical meaning of average, squares, sum of squares, and
centered values.
运行与检查
运行:
python labs\lab04\numbers_summary.py
手动检查:
- 平均值应为
3; - 平方应为
[1, 4, 9, 16, 25]; - 中心化后的值之和应为
0。
提交内容
labs/lab04/numbers_summary.pynotes/lab04-notes.md- PowerShell 或 VS Code 终端输出截图
笔记要求
在 notes/lab04-notes.md 中包括:
- 运行过的命令;
- 两个 Codex 提示词;
- 预期值;
- 输出是否符合预期;
- 一个你手动检查的内容。