Signed-off-by: sairate <sairate@sina.cn>

This commit is contained in:
sairate 2024-11-15 15:57:35 +08:00
commit 5f0b9e5878
11 changed files with 484 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

10
.idea/answer.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (answer)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxBlameSettings">
<option name="version" value="2" />
</component>
</project>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (answer)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/answer.iml" filepath="$PROJECT_DIR$/.idea/answer.iml" />
</modules>
</component>
</project>

0
README.md Normal file
View File

172
main.py Normal file
View File

@ -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)

109
templates/index.html Normal file
View File

@ -0,0 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>答题系统</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
color: #333;
}
.container {
width: 100%;
max-width: 700px;
margin: 50px auto;
padding: 30px;
background-color: #fff;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
h1 {
text-align: center;
color: #4CAF50;
}
h2 {
color: #333;
margin-bottom: 20px;
}
form {
margin-top: 20px;
}
.question-box {
background-color: #f9f9f9;
padding: 15px;
margin-bottom: 15px;
border-radius: 8px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.question-box label {
font-weight: bold;
font-size: 16px;
}
.question-box input {
width: 100%;
padding: 10px;
margin-top: 5px;
border-radius: 5px;
border: 1px solid #ddd;
font-size: 16px;
}
.question-box input:focus {
border-color: #4CAF50;
outline: none;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 15px 20px;
width: 100%;
font-size: 18px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #45a049;
}
a {
display: inline-block;
text-align: center;
font-size: 16px;
color: #4CAF50;
margin-top: 20px;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>欢迎来到答题系统</h1>
<form action="{{ url_for('submit_answer') }}" method="post">
<h2>请回答以下问题:</h2>
{% for question in question_list %}
<div class="question-box">
<label for="answer{{ loop.index }}">问题{{ loop.index }}: {{ question['问题'] }}</label><br>
<input type="text" id="answer{{ loop.index }}" name="answer{{ loop.index }}" required><br><br>
</div>
{% endfor %}
<input type="submit" value="提交答案">
</form>
<div style="margin-top: 20px;">
<a href="{{ url_for('ranking') }}">查看排行榜</a> |
<a href="{{ url_for('logout') }}">注销</a>
</div>
</div>
</body>
</html>

82
templates/login.html Normal file
View File

@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 400px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #333;
}
form {
display: flex;
flex-direction: column;
}
label {
font-size: 16px;
color: #555;
margin-bottom: 5px;
}
input[type="text"],
input[type="password"] {
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
width: 100%;
box-sizing: border-box;
}
input[type="submit"] {
padding: 10px;
background-color: #4CAF50;
color: white;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.footer {
text-align: center;
margin-top: 20px;
font-size: 14px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<h1>登录</h1>
<form action="{{ url_for('login') }}" method="POST">
<label for="name">用户名:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="登录">
</form>
<div class="footer">
<p>&copy; 2024 答题系统</p>
</div>
</div>
</body>
</html>

76
templates/ranking.html Normal file
View File

@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>排行榜</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #333;
}
ul {
list-style: none;
padding: 0;
margin: 20px 0;
}
li {
background-color: #f9f9f9;
padding: 10px;
border-radius: 4px;
margin-bottom: 10px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
font-size: 18px;
display: flex;
justify-content: space-between;
}
li:nth-child(odd) {
background-color: #f1f1f1;
}
a {
display: block;
text-align: center;
margin-top: 20px;
font-size: 18px;
padding: 10px;
background-color: #4CAF50;
color: white;
text-decoration: none;
border-radius: 4px;
transition: background-color 0.3s;
}
a:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>排行榜</h1>
<ul>
{% for user in users %}
<li>
<span>{{ user.name }}</span>
<span>分数: {{ user.score }}</span>
</li>
{% endfor %}
</ul>
<a href="{{ url_for('index') }}">返回首页</a>
</div>
</body>
</html>