📅  最后修改于: 2023-12-03 15:37:02.805000             🧑  作者: Mango
本项目是一个针对小学生的在线测验程序,主要测试小学生对于单位数加法的掌握程度。通过随机生成加法题目,小学生可以在网站上进行测试并查看自己的得分情况。
from flask import Flask, render_template, request
import random
import sqlite3
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
score = 0
for i in range(1, 11):
user_answer = request.form.get(str(i))
if user_answer == str(eval(request.form.get('q'+str(i)))):
score += 10
save_score(request.form.get('name'), score)
return render_template('result.html', name=request.form.get('name'), score=score)
else:
questions = []
for i in range(1, 11):
a = random.randint(1, 9)
b = random.randint(1, 9)
question = str(a) + ' + ' + str(b) + ' = '
questions.append(question)
return render_template('home.html', questions=questions)
def save_score(name, score):
conn = sqlite3.connect('scores.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS scores (name VARCHAR(20), score INT)')
cursor.execute('INSERT INTO scores (name, score) values (?, ?)', (name, score))
conn.commit()
cursor.close()
conn.close()
@app.route('/highscores')
def highscores():
conn = sqlite3.connect('scores.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM scores ORDER BY score DESC LIMIT 10')
rows = cursor.fetchall()
cursor.close()
conn.close()
return render_template('highscores.html', scores=rows)
if __name__ == '__main__':
app.run(debug=True)
python app.py
命令启动程序http://localhost:5000
进行单位数加法在线测验