From 038dcbc2b58fac7a14caeafd7af71da9f777f3b9 Mon Sep 17 00:00:00 2001 From: Zhuang <2157780849@qq.com> Date: Thu, 10 Oct 2024 16:41:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95=E8=AE=BE=E8=AE=A1=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- historyVersion/basicVersion1.py | 53 ++++++++++++++++ historyVersion/basicVersion2.py | 70 +++++++++++++++++++++ historyVersion/connectVersion1.py | 100 ++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 historyVersion/basicVersion1.py create mode 100644 historyVersion/basicVersion2.py create mode 100644 historyVersion/connectVersion1.py diff --git a/historyVersion/basicVersion1.py b/historyVersion/basicVersion1.py new file mode 100644 index 0000000..9978420 --- /dev/null +++ b/historyVersion/basicVersion1.py @@ -0,0 +1,53 @@ +import pandas as pd +import random + +# 读取学生名单 Excel 文件 +def read_student_list(file_path): + df = pd.read_excel(file_path) + df['积分'] = 0 # 初始化每个学生的积分为0 + return df +# 学生名单保存在 students.xlsx 文件中 +students = read_student_list('students.xlsx') +print(students) + + +# 点名规则:按积分计算选中的概率 +def weighted_random_choice(df): + total_weight = sum([1 / (1 + score) for score in df['积分']]) + r = random.uniform(0, total_weight) + upto = 0 + for i, row in df.iterrows(): + weight = 1 / (1 + row['积分']) + if upto + weight >= r: + return i # 返回被点名的学生索引 + upto += weight + +# 更新积分 +def update_score(df, index, answer_correctness): + df.loc[index, 'score'] += 1 # 点到一次+1 + # 根据回答情况调整积分 + if answer_correctness == 'wrong': + df.loc[index, 'score'] -= 1 + elif answer_correctness == 'partial': + df.loc[index, 'score'] += 1 + elif answer_correctness == 'correct': + df.loc[index, 'score'] += 3 +# 示例:更新一个被选中学生的积分 +selected_index = weighted_random_choice(students) +update_score(students, selected_index, 'correct') + +# 保存更新后的名单 +def save_to_excel(df): + df.to_excel("updated_students.xlsx", index=False) +# 示例 +save_to_excel(students) + +# 主循环,随机点名并更新积分 +def classroom_roll_call(file_path, num_sessions): + for _ in range(num_sessions): + selected_index = weighted_random_choice(students) + # 模拟回答情况 + answer = random.choice(['wrong', 'partial', 'correct']) + update_score(students, selected_index, answer) +# 示例:进行10次点名 +classroom_roll_call('students.xlsx', 10) diff --git a/historyVersion/basicVersion2.py b/historyVersion/basicVersion2.py new file mode 100644 index 0000000..b358ac6 --- /dev/null +++ b/historyVersion/basicVersion2.py @@ -0,0 +1,70 @@ +import pandas as pd +import random + +# 加载学生名单,从Excel文件导入 +def load_students(file_path): + df = pd.read_excel(file_path) + # Excel文件有两列:'姓名' 和 '学号' + students = df.to_dict('records') + for student in students: + student['积分'] = 0 # 初始化每位学生的积分为0 + return students + +# 保存学生名单,更新积分后导出到Excel +def save_students(students, file_path): + df = pd.DataFrame(students) + df.to_excel(file_path, index=False) + +# 根据积分设置每个学生的被选概率,积分越高,权重越低 +def get_student_weights(students): + max_score = max([s['积分'] for s in students]) + weights = [(max_score - s['积分'] + 1) for s in students] # 权重为 max_score - 当前积分 + 1 + return weights + +# 随机点名 +def pick_student(students): + weights = get_student_weights(students) + picked_student = random.choices(students, weights=weights, k=1)[0] + print(f"点到学生: {picked_student['姓名']} (学号: {picked_student['学号']})") + return picked_student + +# 增加到达课堂的积分 +def update_attendance_score(student): + student['积分'] += 1 + print(f"{student['姓名']} 到达课堂,积分 +1,总积分: {student['积分']}") + +# 处理回答问题的积分 +def update_answer_score(student, repeated_question_correctly, answer_score): + if repeated_question_correctly: + student['积分'] += 0.5 + print(f"{student['姓名']} 正确重复问题,积分 +0.5") + else: + student['积分'] -= 1 + print(f"{student['姓名']} 未能正确重复问题,积分 -1") + + student['积分'] += answer_score + print(f"{student['姓名']} 回答问题,加 {answer_score} 分,总积分: {student['积分']}") + +def main(): + # Excel 文件路径 + file_path = 'students.xlsx' + + # 加载学生数据 + students = load_students(file_path) + + # 随机点名 + picked_student = pick_student(students) + + # 假设学生到达课堂 客户端反馈是否调用函数 + update_attendance_score(picked_student) + + # 假设学生回答问题 客户端反馈具体答题情况再调用函数 + repeated_correctly = True # 假设学生正确重复了问题 + answer_score = 2.0 # 假设学生回答问题的得分是2分 + update_answer_score(picked_student, repeated_correctly, answer_score) + + # 保存更新后的数据回到 Excel 文件 + save_students(students, file_path) + +if __name__ == '__main__': + main() diff --git a/historyVersion/connectVersion1.py b/historyVersion/connectVersion1.py new file mode 100644 index 0000000..a53f2a0 --- /dev/null +++ b/historyVersion/connectVersion1.py @@ -0,0 +1,100 @@ +from flask import Flask, request, jsonify +import pandas as pd +import random + +app = Flask(__name__) + + +# 加载学生名单,从Excel文件导入 +def load_students(file_path): + df = pd.read_excel(file_path) + students = df.to_dict('records') + return students + + +# 保存学生名单,更新积分后导出到Excel +def save_students(students, file_path): + df = pd.DataFrame(students) + df.to_excel(file_path, index=False) + + +# 根据积分设置每个学生的被选概率,积分越高,权重越低 +def get_student_weights(students): + max_score = max([s['积分'] for s in students]) + weights = [(max_score - s['积分'] + 1) for s in students] + return weights + + +# 随机点名 +def pick_student(students): + weights = get_student_weights(students) + picked_student = random.choices(students, weights=weights, k=1)[0] + return picked_student + + +# 增加到达课堂的积分 +def update_attendance_score(student): + student['积分'] += 1 + return student['积分'] + + +# 处理回答问题的积分 +def update_answer_score(student, repeated_question_correctly, answer_score): + if repeated_question_correctly: + student['积分'] += 0.5 + else: + student['积分'] -= 1 + + student['积分'] += answer_score + return student['积分'] + + +# 文件路径 +file_path = 'students.xlsx' + + +@app.route('/get_random_student', methods=['GET']) +def get_random_student(): + students = load_students(file_path) + picked_student = pick_student(students) + return jsonify({'姓名':picked_student['姓名'], '学号':picked_student['学号']}) + + +@app.route('/update_attendance_score_route', methods=['POST']) +def update_attendance_score_route(): + data = request.json #确定学生到达课堂后 前端传递学号回来 + student_id = data['学号'] + + students = load_students(file_path) + + for student in students: + if student['学号'] == student_id: + updated_score = update_attendance_score(student) + break + + save_students(students, file_path) + + return jsonify({"学号": student_id, "积分": updated_score}) + + +@app.route('/update_answer_score_route', methods=['POST']) +def update_answer_score_route(): + data = request.json + student_id = data['学号'] + repeated_correctly = data['repeated_correctly'] + answer_score = data['answer_score'] + + students = load_students(file_path) + + for student in students: + if student['学号'] == student_id: + updated_score = update_answer_score(student, repeated_correctly, answer_score) + break + + save_students(students, file_path) + + return jsonify({"学号": student_id, "积分": updated_score}) + + +if __name__ == '__main__': + app.run(debug=True)