当前位置:首页>python>Python的chr()方法学习:答题卡分析,自动批改试卷

Python的chr()方法学习:答题卡分析,自动批改试卷

  • 2026-10-11 07:44:20
Python的chr()方法学习:答题卡分析,自动批改试卷

简单几行代码,让电脑帮你改卷子

今天用Python写一个答题卡分析程序,自动批改、统计成绩,省时省力!

先看效果

假设我们有这样一份答题卡:

第1题:学生涂了 A,答案是 A → 正确 ✓第2题:学生涂了 B,答案是 B → 正确 ✓  第3题:学生没涂,答案是 C → 未作答 ?第4题:学生涂了 D,答案是 D → 正确 ✓第5题:学生涂了 C,答案是 E → 错误 ✗

运行程序后,自动输出:

第1题: 考生填涂 A    | 标准答案 A | ✓ 正确第2题: 考生填涂 B    | 标准答案 B | ✓ 正确第3题: 考生填涂 未填涂 | 标准答案 C | 未作答第4题: 考生填涂 D    | 标准答案 D | ✓ 正确第5题: 考生填涂 C    | 标准答案 E | ✗ 错误

核心代码(仅需10行)

# 学生答案(数字代表选项:0=A,1=B,2=C...)student_answers = [0, 1, None, 3, 2]# 标准答案correct_answers = [0, 1, 2, 3, 4]for i, (selected, expected) in enumerate(zip(student_answers, correct_answers), 1):    # 将数字转成字母,未填涂显示"未填涂"    selected_str = f"{chr(65 + selected)}" if selected is not None else "未填涂"    expected_str = f"{chr(65 + expected)}"    if selected is None:        status = "未作答"    elif selected == expected:        status = "✓ 正确"    else:        status = "✗ 错误"    print(f"第{i}题: {selected_str} | {expected_str} | {status}")

关键技术点解析

1. chr()方法:数字转字母

chr(65)  # 返回 'A'chr(66)  # 返回 'B'chr(67)  # 返回 'C'# 以此类推...

为什么从65开始?因为这是ASCII编码中'A'对应的数字。所以我们用chr(65 + 选项数字)就能把0转成A、1转成B。

2. 条件表达式:一行搞定判断

# 完整写法if selected is not None:    selected_str = f"{chr(65 + selected)}"else:    selected_str = "未填涂"# 简化写法(三元表达式)selected_str = f"{chr(65 + selected)}" if selected is not None else "未填涂"

3. None的特殊含义

在Python中,None代表"空"或"没有值"。这里用它表示学生没有填涂这道题。

实战:完整版的答题卡分析系统

def analyze_exam(student_answers, correct_answers):    """分析考试成绩"""    total = len(correct_answers)    correct_count = 0    print("=" * 50)    print("答题卡分析报告")    print("=" * 50)    for i, (selected, expected) in enumerate(zip(student_answers, correct_answers), 1):        selected_str = f"{chr(65 + selected)}" if selected is not None else "未填涂"        expected_str = f"{chr(65 + expected)}"        if selected is None:            status = "❌ 未作答"        elif selected == expected:            status = "✅ 正确"            correct_count += 1        else:            status = "❌ 错误"        print(f"第{i:2d}题: 考生答案 {selected_str:4s} | 标准答案 {expected_str} | {status}")    # 统计结果    score = (correct_count / total) * 100    print("\n" + "=" * 50)    print(f"总计: {total}题 | 正确: {correct_count}题 | 得分: {score:.1f}分")    print("=" * 50)    return score# 测试数据student = [0, 1, None, 3, 2, 0, 4, 1, 2, 3]  # 学生答案answer_key = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]  # 标准答案analyze_exam(student, answer_key)

输出效果:

==================================================答题卡分析报告==================================================第 1题: 考生答案 A    | 标准答案 A | ✅ 正确第 2题: 考生答案 B    | 标准答案 B | ✅ 正确第 3题: 考生答案 未填涂 | 标准答案 C | ❌ 未作答第 4题: 考生答案 D    | 标准答案 D | ✅ 正确第 5题: 考生答案 C    | 标准答案 E | ❌ 错误第 6题: 考生答案 A    | 标准答案 A | ✅ 正确第 7题: 考生答案 E    | 标准答案 B | ❌ 错误第 8题: 考生答案 B    | 标准答案 C | ❌ 错误第 9题: 考生答案 C    | 标准答案 D | ❌ 错误第10题: 考生答案 D    | 标准答案 E | ❌ 错误==================================================总计: 10题 | 正确: 4题 | 得分: 40.0分==================================================

扩展应用场景

1. 多人考试分析

students = {    "张三": [0, 1, 2, 3, 4],    "李四": [0, 1, None, 3, 2],    "王五": [1, 1, 2, 3, 4]}for name, answers in students.items():    score = analyze_exam(answers, answer_key)    print(f"{name} 得分: {score}分\n")

2. 生成Excel报告

import pandas as pd# 创建成绩表data = {    '学生': ['张三', '李四', '王五'],    '得分': [100, 80, 60]}df = pd.DataFrame(data)df.to_excel('考试成绩.xlsx', index=False)

3. 错题本自动生成

wrong_questions = []for i, (selected, expected) in enumerate(zip(student, answer_key), 1):    if selected != expected:        wrong_questions.append({            '题号': i,            '学生答案': chr(65+selected) if selected else '未答',            '正确答案': chr(65+expected)        })print("错题本:", wrong_questions)

小贴士

  1. 1. 选项数量:这种方法支持A-Z共26个选项,够用了吧?
  2. 2. 答题卡识别:实际应用中可以用摄像头拍照+OCR识别,自动提取student_answers
  3. 3. 数据存储:可以用Excel批量管理学生答案和标准答案

结语

用Python批改选择题,不仅省时省力,还能自动生成成绩分析报告。本文介绍的方法简单实用,即使是编程新手也能轻松上手。

如果你想让答题卡分析更智能化(比如自动识别涂卡、生成可视化图表),欢迎关注公众号,后续会推出更多实用教程!


💡 果觉得有用,欢迎点赞、收藏、转发给学习 Python 的朋友。

最新文章

随机文章