Python 题库数据处理:统计、查询、判断功能全实现
以下是一个使用 Python 实现上述功能的示例代码: ```python import os import codecs # 读取题库文件并处理数据 def read_and_process_data(): file_path = '/data/share_files/experiment_file/e_16545/datafiles/python 题库(考试用)' with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() chapter_dict = {} current_chapter = None for line in data: if '章' in line: current_chapter = line.split(':')[0] chapter_dict[current_chapter] = {'选择题': 0, '判断题': 0} elif '选择题' in line: chapter_dict[current_chapter]['选择题'] += 1 elif '判断题' in line: chapter_dict[current_chapter]['判断题'] += 1 return chapter_dict # 根据用户输入输出指定题目 def output_specific_question(chapter, question_type, question_number): file_path = '/data/share_files/experiment_file/e_16545/datafiles/python 题库(考试用)' with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() found = False current_chapter = None question_count = 0 for line in data: if '章' in line: current_chapter = line.split(':')[0] if current_chapter == chapter and question_type in line: question_count += 1 if question_count == question_number: found = True print(line.strip()) break if not found: print("未找到指定题目") # 根据用户输入输出指定题目答案 def output_specific_answer(chapter, question_type, question_number): file_path = '/data/share_files/experiment_file/e_16545/datafiles/python 题库(考试用)' with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() found = False current_chapter = None question_count = 0 for i, line in enumerate(data): if '章' in line: current_chapter = line.split(':')[0] if current_chapter == chapter and question_type in line: question_count += 1 if question_count == question_number: found = True print(data[i + 1].strip()) # 假设答案在题目后一行 break if not found: print("未找到指定题目答案") # 根据用户输入进行题目判断 def judge_question(chapter, question_type, question_number, user_answer): file_path = '/data/share_files/experiment_file/e_16545/datafiles/python 题库(考试用)' with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() found = False current_chapter = None question_count = 0 correct_answer = None for i, line in enumerate(data): if '章' in line: current_chapter = line.split(':')[0] if current_chapter == chapter and question_type in line: question_count += 1 if question_count == question_number: found = True correct_answer = data[i + 1].strip() # 假设答案在题目后一行 break if found: if user_answer == correct_answer: print("正确") else: print("错误") else: print("未找到指定题目") # 测试代码 if __name__ == "__main__": # 统计题目类型和数量 chapter_dict = read_and_process_data() for chapter, types_count in chapter_dict.items(): print(f"{chapter}: 选择题 {types_count['选择题']} 道,判断题 {types_count['判断题']} 道") # 输出指定题目 output_specific_question("第二章", "判断题", 6) # 输出指定题目答案 output_specific_answer("第二章", "判断题", 6) # 进行题目判断 judge_question("第二章", "判断题", 6, "用户输入的答案") ``` 额外需要注意的逻辑点: 1. 确保文件路径的正确性,避免文件读取失败。 2. 对于题目和答案的匹配逻辑,要根据实际题库的格式进行准确处理。 3. 处理用户输入时,要进行适当的错误检查和提示。 ######[AI写代码神器 | 1057点数解答 | 2025-01-05 11:20:25]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)375
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)362
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)399
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)348
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)346
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)293
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)307
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)478
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- Python实战:爬取西部证券股票数据并多方法分析(字节豆包 | 744点数解答 | 2024-11-11 20:45:55)146
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)432
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)426