DMS游戏活动中心 - 热门活动与福利速递

如何用python做一个考试系统

活动专题 2025-11-15 13:28:00

import tkinter as tk

from tkinter import messagebox

import sqlite3

import hashlib

from datetime import datetime

哈希密码

def hash_password(password):

return hashlib.sha256(password.encode()).hexdigest()

存储用户信息

def store_user(username, password):

conn = sqlite3.connect('exam.db')

c = conn.cursor()

hashed_password = hash_password(password)

c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))

conn.commit()

conn.close()

登录验证

def login():

username = entry_username.get()

password = entry_password.get()

hashed_password = hash_password(password)

conn = sqlite3.connect('exam.db')

c = conn.cursor()

c.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, hashed_password))

user = c.fetchone()

conn.close()

if user:

messagebox.showinfo("登录成功", "欢迎使用考试系统")

exam_window(user[0])

else:

messagebox.showerror("登录失败", "用户名或密码错误")

计算分数

def calculate_score(user_answers):

conn = sqlite3.connect('exam.db')

c = conn.cursor()

c.execute("SELECT * FROM questions")

questions = c.fetchall()

score = 0

for i, question in enumerate(questions):

if user_answers[i] == question[6]:

score += 1

conn.close()

return score

考试界面

def exam_window(user_id):

exam = tk.Toplevel(root)

exam.title("考试界面")

tk.Label(exam, text="考试内容").pack()

conn = sqlite3.connect('exam.db')

c = conn.cursor()

c.execute("SELECT * FROM questions")

questions = c.fetchall()

conn.close()

user_answers = []

def submit_exam():

score = calculate_score(user_answers)

conn = sqlite3.connect('exam.db')

c = conn.cursor()

c.execute("INSERT INTO exam_records (user_id, score, date) VALUES (?, ?, ?)", (user_id, score, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))

conn.commit()

conn.close()

messagebox.showinfo("考试结束", f"您的得分是:{score}")

exam.destroy()

for i, question in enumerate(questions):

tk.Label(exam, text=question[1]).pack()

var = tk.StringVar()

user_answers.append(var)

for j in range(2, 6):

tk.Radiobutton(exam, text=question[j], variable=user_answers[i], value=question[j]).pack()

tk.Button(exam, text="提交", command=submit_exam).pack()

主窗口

root = tk.Tk()

root.title("考试系统")

登录界面

tk.Label(root, text="用户名").grid(row=0)

tk.Label(root, text="密码").grid(row=1)

entry_username = tk.Entry(root)

entry_password = tk.Entry(root, show="*")

entry_username.grid(row=0, column=1)

entry_password.grid(row=1, column=1)

tk.Button(root, text="登录", command=login).grid(row=2, column=1)

初始化数据库

def init_db():

conn = sqlite3.connect('exam.db')

c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS users

(id INTEGER PRIMARY KEY, username TEXT, password TEXT)''')

c.execute('''CREATE TABLE IF NOT EXISTS questions

(id INTEGER PRIMARY KEY, question TEXT, option1 TEXT, option2 TEXT, option3 TEXT, option4 TEXT, answer TEXT)''')

c.execute('''CREATE TABLE IF NOT EXISTS exam_records

(id INTEGER PRIMARY KEY, user_id INTEGER, score INTEGER, date TEXT, FOREIGN KEY(user_id) REFERENCES users(id))''')

c.execute("INSERT INTO questions (question, option1, option2, option3, option4, answer) VALUES (?, ?, ?, ?, ?, ?)",

("Python的作者是谁?", "Guido van Rossum", "Linus Torvalds", "James Gosling", "Dennis Ritchie", "Guido van Rossum"))

conn.commit()

conn.close()

init_db()

root.mainloop()