You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Python基础知识点考试系统
# 功能:题库查看、随机抽题考试、成绩统计与错题分析
# 覆盖知识点:函数、循环、条件判断、列表/字典、文件操作、异常处理
import random
import time
# 初始化题库(使用列表嵌套字典存储,键:题目、选项、正确答案、知识点)
question_bank = [
{
"question": "Python中定义函数的关键字是",
"options": ["A. func", "B. def", "C. function", "D. define"],
"answer": "B",
"knowledge": "函数定义"
},
{
"question": "以下哪个不是Python的基本数据类型",
"options": ["A. int", "B. str", "C. list", "D. array"],
"answer": "D",
"knowledge": "数据类型"
},
{
"question": "执行print(10 % 3)的结果是?",
"options": ["A. 3", "B. 1", "C. 0", "D. 3.33"],
"answer": "B",
"knowledge": "算术运算符"
},
{
"question": "Python中循环语句不包含以下哪个",
"options": ["A. for", "B. while", "C. do...while", "D. 以上都不是"],
"answer": "C",
"knowledge": "循环结构"
},
{
"question": "字典的键值对使用什么符号分隔?",
"options": ["A. 逗号", "B. 冒号", "C. 等号", "D. 分号"],
"answer": "B",
"knowledge": "字典类型"
},
{
"question": "if语句后的条件表达式结尾需要加什么符号",
"options": ["A. 分号", "B. 冒号", "C. 逗号", "D. 不需要"],
"answer": "B",
"knowledge": "条件判断"
},
{
"question": "列表的索引从几开始?",
"options": ["A. 0", "B. 1", "C. -1", "D. 任意"],
"answer": "A",
"knowledge": "列表操作"
},
{
"question": "打开文件的函数是?",
"options": ["A. open()", "B. read()", "C. file()", "D. write()"],
"answer": "A",
"knowledge": "文件操作"
}
]
def show_menu():
"""显示系统主菜单"""
print("\n===== Python基础知识点考试系统 =====")
print("1. 查看题库")
print("2. 开始考试")
print("3. 退出系统")
print("====================================")
def view_question_bank():
"""查看完整题库功能"""
print("\n===== Python基础题库 =====")
for idx, question in enumerate(question_bank, 1):
print(f"\n{idx}. {question['question']}")
print(" 选项:", " | ".join(question['options']))
print(f" 正确答案:{question['answer']}")
print(f" 知识点:{question['knowledge']}")
input("\n按回车键返回主菜单...")
def start_exam():
"""开始考试核心功能:随机抽题、限时答题、实时判分"""
# 1. 设置考试参数
exam_time = 5 # 每题答题时间(秒),可调整
question_num = 5 # 抽取题目数量,可调整
# 2. 随机抽取题目(避免重复)
if question_num > len(question_bank):
print(f"题库仅有{len(question_bank)}题,自动调整为全部题目考试")
question_num = len(question_bank)
selected_questions = random.sample(question_bank, question_num)
# 3. 初始化考试数据
score = 0 # 总分
wrong_questions = [] # 存储错题
total_points = question_num * 20 # 总分100分每题20分
print(f"\n===== 考试开始 =====\n本次考试共{question_num}题,每题{exam_time}秒答题时间,总分{total_points}")
print("请输入选项A/B/C/D输入错误将直接判定为答错")
# 4. 逐题答题
for idx, q in enumerate(selected_questions, 1):
print(f"\n{idx}题({exam_time}秒):{q['question']}")
print(" 选项:", " | ".join(q['options']))
# 限时答题
try:
start_time = time.time()
user_answer = input("请输入你的答案:").strip().upper()
used_time = time.time() - start_time
if used_time > exam_time:
print(f"答题超时(用时{used_time:.1f}秒),本题不得分!")
wrong_questions.append({
"question": q['question'],
"user_answer": "超时未答",
"correct_answer": q['answer']
})
continue
# 判分
if user_answer == q['answer']:
print("回答正确!")
score += 20
else:
print(f"回答错误!正确答案是:{q['answer']}")
wrong_questions.append({
"question": q['question'],
"user_answer": user_answer,
"correct_answer": q['answer']
})
except:
print("输入异常,本题判定为答错!")
wrong_questions.append({
"question": q['question'],
"user_answer": "输入异常",
"correct_answer": q['answer']
})
# 5. 成绩统计与展示
show_score(score, total_points, wrong_questions)
# 6. 保存成绩到文件
save_score(score, total_points, wrong_questions)
def show_score(score, total_points, wrong_questions):
"""成绩统计与错题展示功能"""
print("\n===== 考试结束 ======")
print(f"你的最终得分:{score}分(总分{total_points}分)")
accuracy = (score / total_points) * 100 if total_points > 0 else 0
print(f"答题正确率:{accuracy:.1f}%")
# 展示错题
if wrong_questions:
print(f"\n===== 错题分析(共{len(wrong_questions)}题)=====")
for idx, wq in enumerate(wrong_questions, 1):
print(f"\n{idx}. 题目:{wq['question']}")
print(f" 你的答案:{wq['user_answer']}")
print(f" 正确答案:{wq['correct_answer']}")
else:
print("\n恭喜!你全部答对了!")
input("\n按回车键返回主菜单...")
def save_score(score, total_points, wrong_questions):
"""将考试成绩保存到文件(文件操作)"""
try:
with open("exam_result.txt", "a", encoding="utf-8") as f:
f.write(f"\n===== 考试成绩记录 =====\n")
f.write(f"考试时间:{time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"得分:{score}分(总分{total_points}分)\n")
f.write(f"正确率:{(score / total_points) * 100:.1f}%\n")
if wrong_questions:
f.write(f"错题数量:{len(wrong_questions)}\n")
for wq in wrong_questions:
f.write(f"题目:{wq['question']} | 你的答案:{wq['user_answer']} | 正确答案:{wq['correct_answer']}\n")
print("成绩已保存到 exam_result.txt 文件!")
except:
print("成绩保存失败,请检查文件权限!")
def main():
"""系统主函数:控制整体流程"""
while True:
show_menu()
try:
choice = int(input("请输入功能编号1-3"))
if choice == 1:
view_question_bank()
elif choice == 2:
start_exam()
elif choice == 3:
print("感谢使用Python考试系统再见")
break
else:
print("输入错误请输入1-3之间的数字")
except ValueError:
print("输入格式错误,请输入数字!")
# 程序入口
if __name__ == "__main__":
main()