From 5f0b9e5878c8cf290d9f5f6e8c7ccf0a4df79ee7 Mon Sep 17 00:00:00 2001 From: sairate Date: Fri, 15 Nov 2024 15:57:35 +0800 Subject: [PATCH] Signed-off-by: sairate --- .idea/.gitignore | 8 + .idea/answer.iml | 10 + .idea/git_toolbox_blame.xml | 6 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + README.md | 0 main.py | 172 ++++++++++++++++++ templates/index.html | 109 +++++++++++ templates/login.html | 82 +++++++++ templates/ranking.html | 76 ++++++++ 11 files changed, 484 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/answer.iml create mode 100644 .idea/git_toolbox_blame.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 README.md create mode 100644 main.py create mode 100644 templates/index.html create mode 100644 templates/login.html create mode 100644 templates/ranking.html diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/answer.iml b/.idea/answer.iml new file mode 100644 index 0000000..5458cfd --- /dev/null +++ b/.idea/answer.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml new file mode 100644 index 0000000..7dc1249 --- /dev/null +++ b/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6b90e96 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5121bbd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..a9f48a7 --- /dev/null +++ b/main.py @@ -0,0 +1,172 @@ +from flask import Flask, render_template, request, redirect, url_for, session +import random + +app = Flask(__name__) +app.secret_key = 'supersecretkey' # 用于 session 加密 + + +class db_base(): + # 存储多个问题和答案 + ask_answer = [ + {"问题": """ + 面向对象的三大特点? + A.无序,唯一,封装 B.继承,多态,封装 C.继承,无序,多态 + """ + , "答案": "B"}, + {"问题": """ + 程序的运行结果? + a=5 + def solve(a): + if a==2: + return 0 + print(a) + for i in range(1,a+1): + sol = solve(i) + return sol + print(solve(a)) + """ + , "答案": ""}, + {"问题": "问题3", "答案": "C"} + ] + table1_db = [{"name": "战神战无不胜", "password": "123456", "score": 0}] # 初始用户数据 + + def __init__(self, name="数据库"): + self.name = name + + def add(self, name, password, score=0): + """ 添加新用户 """ + self.table1_db.append({"name": name, "password": password, "score": score}) + + def show(self): + """ 返回所有用户信息 """ + return self.table1_db + + def change(self, name, score): + """ 修改用户的分数 """ + for i in self.table1_db: + if i["name"] == name: + i["score"] = score + return + + def fliter(self): + """ 排序并返回按分数降序排列的用户列表 """ + score_list = sorted(self.table1_db, key=lambda x: x["score"], reverse=True) + return score_list + + def ask_question(self, name, answers): + """ 用户答题并更新分数 """ + score = 0 + # 遍历所有问题,检查答案 + for i, question in enumerate(self.ask_answer): + correct_answer = question["答案"] + if answers[i] == correct_answer: + score += 3 # 每个问题答对得10分 + self.change(name, score) + + def authenticate(self, name, password): + """ 验证用户名和密码 """ + for user in self.table1_db: + if user["name"] == name and user["password"] == password: + return True + return False + + + +# 初始化数据库 +db = db_base() + +# 常见的昵称词汇 +adjectives = ["快乐", "微笑", "温", "亮", "甜美", "阳光", "皮", "萌萌", "幸福", "自由", "聪明", "勇敢", "帅气", "人", "光", "安静", "甜蜜"] +nouns = ["小猫", "狗", "熊", "星星", "月亮", "飞鸟", "星辰", "玫瑰", "蓝天", "云", "海洋", "沙滩", "花", "苹果", "橙子", "草地", "心情"] +colors = ["红", "蓝", "紫", "绿", "黄", "粉", "黑", "白", "银", "金", "橙", "青"] + +# 组合生成昵称 +def generate_random_nickname(): + adj = random.choice(adjectives) # 从形容词中选择 + noun = random.choice(nouns) # 从名词中选择 + color = random.choice(colors) # 从颜色中选择 + nickname = adj + color + noun # 组合成昵称 + return nickname + +# 添加10个随机用户 +for i in range(9): + name = str(generate_random_nickname()) + password = str(random.randint(111111, 999999)) + score = random.randint(0, 100)*3 + db.add(name, password, score) + +# 首页,展示问题和表单 +@app.route('/') +def index(): + # 检查用户是否登录 + if 'username' not in session: + return redirect(url_for('login')) # 未登录则跳转到登录页面 + + # 提供所有问题 + question_list = db.ask_answer + return render_template('index.html', question_list=question_list) + + +# 登录页面 +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + name = request.form['name'] + password = request.form['password'] + # 验证用户名和密码 + if db.authenticate(name, password): + session['username'] = name # 将用户名存入 session + return redirect(url_for('index')) # 登录成功后跳转到首页 + else: + return "登录失败,用户名或密码错误!" # 登录失败提示 + return render_template('login.html') + +# 提交答案 +@app.route('/submit_answer', methods=['POST']) +def submit_answer(): + if 'username' not in session: + return redirect(url_for('login')) # 用户未登录时跳转到登录页面 + + name = session['username'] # 获取当前登录的用户名 + answers = [] + + # 获取所有问题的答案 + for i in range(1, len(db.ask_answer) + 1): + answer = request.form.get(f"answer{i}") + answers.append(answer) + + # 用户回答所有问题 + db.ask_question(name, answers) + + # 跳转到排行榜页面 + return redirect(url_for('ranking')) + + +# 排行榜页面 +@app.route('/ranking') +def ranking(): + if 'username' not in session: + return redirect(url_for('login')) # 用户未登录时跳转到登录页面 + + # 获取排序后的成绩 + sorted_users = db.fliter() + return render_template('ranking.html', users=sorted_users) + +# 添加随机用户 +@app.route('/add_users') +def add_users(): + for _ in range(10): + name = str(random.randint(10, 100)) + password = str(random.randint(111111, 999999)) + score = random.randint(1, 40) + db.add(name, password, score) + return redirect(url_for('index')) + +# 注销登录 +@app.route('/logout') +def logout(): + session.pop('username', None) # 删除 session 中的用户名 + return redirect(url_for('login')) # 跳转到登录页面 + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..82e01bc --- /dev/null +++ b/templates/index.html @@ -0,0 +1,109 @@ + + + + + + 答题系统 + + + + +
+

欢迎来到答题系统

+
+

请回答以下问题:

+ + {% for question in question_list %} +
+
+

+
+ {% endfor %} + + +
+ +
+ 查看排行榜 | + 注销 +
+
+ + + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..2ce76f5 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,82 @@ + + + + + + 登录 + + + +
+

登录

+
+ +

+ +

+ +
+ +
+ + diff --git a/templates/ranking.html b/templates/ranking.html new file mode 100644 index 0000000..a04e95e --- /dev/null +++ b/templates/ranking.html @@ -0,0 +1,76 @@ + + + + + + 排行榜 + + + +
+

排行榜

+
    + {% for user in users %} +
  • + {{ user.name }} + 分数: {{ user.score }} +
  • + {% endfor %} +
+ 返回首页 +
+ +