首页 > 其他分享 >4.27

4.27

时间:2024-06-19 18:12:01浏览次数:16  
标签:试题 writer question questions 4.27 qtype row

题目;

题目 14 标准化考试系统设计 【设计要求】功能要求用文件保存试题库。(要求试题类型覆盖选择、判断、 填空等客观题类型)实现试题增加、删除、修改、查询等功能,并要求实现自动 组卷并输出到试卷文件 【界面要求】要求图形界面实现。

代码:

import os
import tkinter as tk
from collections import defaultdict
from tkinter import messagebox, simpledialog
import csv
import random

CSV_FILE_PATH = "questions.csv"


def init_csv_file():
if not os.path.exists(CSV_FILE_PATH):
with open(CSV_FILE_PATH, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"])


def read_all_questions():
"""读取CSV文件中的所有试题信息"""
with open(CSV_FILE_PATH, mode='r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过表头
questions = [row for row in reader]
return questions


def add_question(id, question, qtype, answer):
"""向CSV文件中添加一条新的试题记录"""
with open(CSV_FILE_PATH, mode='a', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([id, question, qtype, answer])


def delete_question(qid):
"""根据试题编号从CSV文件中删除一条试题记录"""
questions = read_all_questions()
updated_questions = [row for row in questions if row[0] != qid]

# 清空原文件并写入更新后的数据
with open(CSV_FILE_PATH, mode='w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"]) # 写入表头
writer.writerows(updated_questions)


def update_question(qid, new_content=None, new_qtype=None, new_answer=None):
"""根据试题编号更新试题内容"""
questions = read_all_questions()
updated = False

for row in questions:
if row[0] == qid:
if new_content is not None:
row[1] = new_content
if new_qtype is not None:
row[2] = new_qtype
if new_answer is not None:
row[3] = new_answer
updated = True

if updated:
# 更新后重新写入文件
with open(CSV_FILE_PATH, mode='w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"]) # 写入表头
writer.writerows(questions)
else:
messagebox.showerror("错误", "未找到指定编号的试题")


def search_question(query):
"""根据查询内容搜索试题,支持题干、类型、答案的模糊搜索"""
questions = read_all_questions()
results = [row for row in questions if query.lower() in (row[1].lower() or '') or
query.lower() in (row[2].lower() or '') or
query.lower() in (row[3].lower() or '')]
return results


def add_question_gui():
question = simpledialog.askstring("新增试题", "请输入题干:")
answer = simpledialog.askstring("新增试题", "请输入答案:")
qtype = simpledialog.askstring("新增试题", "请输入题型:")
new_id = max([int(row[0]) for row in read_all_questions()] or [0]) + 1
add_question(str(new_id), question, qtype, answer)
messagebox.showinfo("成功", "试题添加成功!")


def delete_question_gui():
qid = simpledialog.askstring("删除试题", "请输入试题编号:")
delete_question(qid)
messagebox.showinfo("成功", "试题删除成功!")


def update_question_gui():
qid = simpledialog.askstring("更新试题", "请输入试题编号:")
question = simpledialog.askstring("更新试题", "请输入新的题干(留空则不修改):", initialvalue="")
answer = simpledialog.askstring("更新试题", "请输入新的答案(留空则不修改):", initialvalue="")
qtype = simpledialog.askstring("更新试题", "请输入新的题型(留空则不修改):", initialvalue="")
update_question(qid, new_content=question, new_qtype=qtype, new_answer=answer)
messagebox.showinfo("成功", "试题更新成功!")


def search_question_gui():
query = simpledialog.askstring("查询试题", "请输入查询内容:")
result = search_question(query)
if result:
messagebox.showinfo("查询结果", "\n".join([" ".join(row) for row in result]))
else:
messagebox.showinfo("查询结果", "未找到相关试题")


def generate_paper_gui():
# 用户输入题型和数量信息
question_quantities = defaultdict(int)
question_types = ["选择题", "判断题", "填空题"]
for qtype in question_types:
quantity = simpledialog.askinteger("生成试卷", f"请输入{qtype}的题数:")
question_quantities[qtype] = quantity

generate_paper(question_quantities)


def generate_paper(question_quantities):
questions = read_all_questions()
paper_questions = []
current_question_num = 1

for qtype, quantity in question_quantities.items():
type_questions = [q for q in questions if q[2] == qtype]

if len(type_questions) >= quantity:
selected_questions = random.sample(type_questions, quantity)
for q in selected_questions:
paper_questions.append([current_question_num, q[1]]) # 添加题号并加入试卷
current_question_num += 1
else:
messagebox.showwarning("警告", f"题库中{qtype}的数量不足{quantity},无法生成试卷。")
return

# 写入生成的试卷题干到新文件中
with open("generated_paper.csv", 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["题号", "题干"])
writer.writerows(paper_questions)


def create_widgets():
btn_add = tk.Button(root, text="添加试题", command=add_question_gui)
btn_add.pack(pady=10)

btn_delete = tk.Button(root, text="删除试题", command=delete_question_gui)
btn_delete.pack(pady=10)

btn_update = tk.Button(root, text="更新试题", command=update_question_gui)
btn_update.pack(pady=10)

btn_search = tk.Button(root, text="查询试题", command=search_question_gui)
btn_search.pack(pady=10)

btn_generate_paper = tk.Button(root, text="生成试卷", command=generate_paper_gui)
btn_generate_paper.pack(pady=10)


def gui_init():
global root
root = tk.Tk()
root.title("试题库管理系统")
init_csv_file()
create_widgets()
root.mainloop()


gui_init()

标签:试题,writer,question,questions,4.27,qtype,row
From: https://www.cnblogs.com/szm123/p/18256970

相关文章

  • 2024年6月 AWVS -24.4.27详细安装教程附下载教程含windows和linux多版本
    免责声明请勿利用文章内的相关技术从事非法测试。由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,作者不为此承担任何责任,请务必遵守网络安全法律法规。本文仅用于测试,请完成测试后24小时删除,请勿用于商业用途。如文中内容涉及侵权......
  • 4.27
    改正邮箱验证码的小bug```ValueOperations<String,String>operations=stringRedisTemplate.opsForValue();StringverifyCode=operations.get("verify");if(verifyCode==null){returnResult.error("请获取验证码");}if(!verifyCode.equals(veri......
  • 2024.04.27 笔记,下午
    b/s架构和c/s架构(重点)(1)bs:浏览器------服务器(web)b:broeser浏览器s:server服务器bs的应用:论坛,百度,知乎,豆瓣,csdn,博客园(2)cs架构:客户端-----服务器(app)c:client客户端s:server服务器cs应用:抖音,微信,qq,快手,酷狗区别:(1)bs不需要更新,直接通过浏览器输入网址进行访问;......
  • 今日模拟前端面试10道题 看你能答对几道 24.4.27
    1.介绍Promise的特性,优缺点Promise是JavaScript中用于处理异步操作的一种对象。Promise的特性:状态:Promise有三种状态,分别是pending(进行中)、fulfilled(已成功)和rejected(已失败)。不可逆性:一旦Promise的状态改变,就不能再被修改,无论是从pending变为fulfilled还是从pending变为reje......
  • UE4.27, 代码实践, 资源加载 FSoftClassPath / FSoftObjectPath
    //以下的FSoftClassPath/FSoftObjectPath都公开在Editor里设定 //iteminTArray<FSoftClassPath>FSoftClassPathtemp=FSoftClassPath_UsedInBluePrint_BuildFunc(item);UClass*LoadedClass=temp.TryLoadClass<AActor>();FSoftClassPathFSoftClassPath_UsedInBlu......
  • UE4.27 bug汇总
    编译时errorC4668:没有将“_WIN32_WINNT_WIN10_TH2”定义为预处理器宏,用“0”替换“#if/#elif”一般为Windows中的宏和UE4冲突所致,需要用如下头文件包裹冲突的头文件:#include"Windows/AllowWindowsPlatformTypes.h"#include"Windows/PreWindowsApi.h"#include"冲突的......
  • UE4.27, Debug issues, "变量已被优化掉,因而不可用"
    调试时添加监控后,变量未被成功监控,显示"变量已被优化掉,因而不可用" 所使用的解决办法从解决方案配置的下拉菜单中选择DebugGameEditor  感谢阅读,敬请斧正......
  • UE4.27, Packaging failed, "is found in memory and is an export but does not have
    打包时发生如下错误"isfoundinmemoryandisanexportbutdoesnothaveallloadflags"通过查阅论坛,问题原因出在,某类的构造函数里包含xxx->SetChildActorClass(ActualOne) 整理到的解决办法并未完全验证1,愿意手动设定ChildActorComponent的话,将原代码改为xxx->SetC......
  • 建民打卡日记4.27
    一、问题描述“福”字倒着贴,寓意“福到”。不论到底算不算民俗,本题且请你编写程序,把各种汉字倒过来输出。这里要处理的每个汉字是由一个N × N的网格组成的,网格中的元素或者为字符 @ 或者为空格。而倒过来的汉字所用的字符由裁判指定。二、流程设计首先逐行录入;双重for......
  • KubeSphere 社区双周报 | 杭州站 Meetup 议题征集中 | 2023.04.14-04.27
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.04.14-2023.04.27。贡献者名单新晋KubeSphereCon......