我创建了一个带有测验的电子学习模块。然而,当用户完成后,它仅显示每个模块中的一个分数。 这是例子:
当我在模块 1 中进行测验时显示: 网络基础知识:分数 - 10 网络工具和测试设备:得分 - 0 网络电缆:得分-0
当我在模块 2 中进行测验时显示: 网络基础知识:分数 - 0 网络工具和测试设备:分数 - 10 网络电缆:得分 - 0
当我在模块 3 中进行测验时显示: 网络基础知识:分数 - 0 网络工具和测试设备:得分 - 0 网络电缆:得分 - 7
应该是这样的:
当我在模块 1 中进行测验时: 网络基础知识:分数 - 10 网络工具和测试设备:得分 - 0 网络电缆:得分-0
当我在模块 2 中进行测验时显示: 网络基础知识:分数 - 10 网络工具和测试设备:分数 - 10 网络电缆:得分 - 0
当我在模块 3 中进行测验时显示: 网络基础知识:分数 - 10 网络工具和测试设备:分数 - 10 网络电缆:得分 - 7
这是我的代码:
@app.route("/提交", 方法=["POST", "GET"]) def Submit_quiz(): 全球电子邮件 全局测验_尝试
users = []
attempts = []
scores = {'Fundamentals of Networking': 0, 'Network Tools and Testing Devices': 0, 'Network Cables': 0}
quiz_files = ["questions1.txt", "questions2.txt", "questions3.txt"]
for idx, quiz_file in enumerate(quiz_files):
quiz = []
questions_file = open(quiz_file, "r")
questions = questions_file.read().splitlines()
questions_file.close()
number = 0
for number, question in enumerate(questions):
question_object = create_question_object(question, number)
quiz.append(question_object)
for idx in range(0, len(quiz)):
mcq = "mcq" + str(idx + 1)
attempts.append(request.form.get(mcq))
print("Length of quiz:", len(quiz))
print("Length of attempts:", len(attempts))
if quiz_file == "questions1.txt":
quiz_name = "Fundamentals of Networking"
elif quiz_file == "questions2.txt":
quiz_name = "Network Tools and Testing Devices"
else:
quiz_name = "Network Cables"
for idx in range(0, len(quiz)):
if quiz[idx].correct == attempts[idx]:
scores[quiz_name] += 1
users_data_file = open("users_data.txt", "r")
users = users_data_file.read().splitlines()
users_data_file.close()
for idx in range(0, len(users)):
if email == get_field(users[idx], 1):
users[idx] = (
str(get_field(users[idx], 0))
+ ","
+ str(get_field(users[idx], 1))
+ ","
+ str(get_field(users[idx], 2))
+ ","
+ str(scores["Fundamentals of Networking"])
+ ","
+ str(scores["Network Tools and Testing Devices"])
+ ","
+ str(scores["Network Cables"])
)
user_data_file = open("users_data.txt", "w")
for user in users:
print(user, file=user_data_file, sep="\n")
user_data_file.close()
return render_template("user.html")
@app.route("/show", method=["POST", "GET"]) 定义结果(): 全球电子邮件 用户=[]
users_data_file = open("users_data.txt", "r")
users = users_data_file.read().splitlines()
users_data_file.close()
# Initialize dictionaries to store scores for each quiz
scores = {
'Fundamentals of Networking': 0,
'Network Tools and Testing Devices': 0,
'Network Cables': 0
}
for user in users:
# Check if the user's email matches
if email == get_field(user, 1):
# Update scores for each quiz from the user's data
scores['Fundamentals of Networking'] = get_field(user, 3)
scores['Network Tools and Testing Devices'] = get_field(user, 4)
scores['Network Cables'] = get_field(user, 5)
return render_template("result.html", scores=scores)
当用户访问端点提交时,您似乎将他们的分数初始化为
{'Fundamentals of Networking': 0, 'Network Tools and Testing Devices': 0, 'Network Cables': 0}
尽管字典中没有读取和写入先前存储的分数,但会将其旧分数覆盖为 0。
旁注:我可以推荐使用
with open("users_data.txt", "w") as user_data_file :
for user in users:
user_data_file.write(user)
而不是手动打开和关闭文件?