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.

91 lines
3.1 KiB

import pandas as pd
import random
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# 读取Excel文件并初始化学生数据
def load_students(file_path):
df = pd.read_excel(file_path, engine='openpyxl') # 指定引擎
students = [{'id': row['学号'], 'name': row['姓名'], 'points': 0} for index, row in df.iterrows()]
return students
# 随机点名算法,积分越高,概率越低
def weighted_random_choice(students):
total_weights = sum(1 / (s['points'] + 1) for s in students)
random_value = random.uniform(0, total_weights)
current_weight = 0
for student in students:
weight = 1 / (student['points'] + 1)
if current_weight + weight >= random_value:
return student
current_weight += weight
return students[-1]
# 点名并到达课堂,积分 +1
def attend_class(student):
student['points'] += 1
# 回答问题并进行加减分
def answer_question(student, accurate_repeat, answer_score):
if accurate_repeat:
student['points'] += 0.5
else:
student['points'] -= 1
student['points'] += answer_score
# 加载学生数据
students = load_students(r'C:\Users\86152\Desktop\新建文件夹 (2)\新建文件夹 (2)\软工学生名单.xlsx')
@app.route('/')
def index():
return render_template('index.html', student={})
@app.route('/call_student')
def call_student():
student = weighted_random_choice(students)
attend_class(student)
return render_template('index.html', student=student)
@app.route('/ask_question', methods=['GET', 'POST'])
def ask_question():
if request.method == 'POST':
# 获取表单传递的 student_id
student_id = request.form.get('student_id')
# 调试打印学号以确保正确传递
print(f"传递的学号: {student_id}")
# 获取前端传递的是否准确重复问题和得分
accurate_repeat = request.form.get('accurate_repeat') == 'on'
answer_score = float(request.form.get('answer_score', 0))
# 处理学号为字符串以确保类型一致
try:
# 确保学号一致时,将 student_id 转换为字符串
student = next(s for s in students if str(s['id']) == str(student_id))
# 更新学生积分
answer_question(student, accurate_repeat, answer_score)
except StopIteration:
# 如果找不到对应学号的学生,返回提示信息
return "未找到对应学号的学生,请检查输入。"
return redirect(url_for('index'))
# 在 GET 请求中随机选择一个学生
student = weighted_random_choice(students)
# 返回带有学生数据的模板
return render_template('ask_question.html', student=student)
@app.route('/rankings')
def rankings():
sorted_students = sorted(students, key=lambda s: s['points'], reverse=True)
return render_template('rankings.html', rankings=sorted_students)
if __name__ == "__main__":
app.run(debug=True)