|
from flask import Flask, render_template, request, session, redirect, url_for |
|
import random |
|
|
|
app = Flask(__name__) |
|
app.secret_key = "your_secret_key_here" |
|
|
|
|
|
element_outer_electrons: dict[str, int] = { |
|
"水素": 1, |
|
"ヘリウム": 2, |
|
"リチウム": 1, |
|
"ベリリウム": 2, |
|
"ホウ素": 3, |
|
"炭素": 4, |
|
"窒素": 5, |
|
"酸素": 6, |
|
"フッ素": 7, |
|
"アルゴン": 8, |
|
"クリプトン": 8, |
|
"キセノン": 8, |
|
"ナトリウム": 1, |
|
"マグネシウム": 2, |
|
"アルミニウム": 3, |
|
"ケイ素": 4, |
|
"リン": 5, |
|
"硫黄": 6, |
|
"塩素": 7, |
|
"カリウム": 1, |
|
"カルシウム": 2, |
|
|
|
} |
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
def quiz(): |
|
|
|
if "element" not in session: |
|
session["element"] = random.choice(list(element_outer_electrons.keys())) |
|
|
|
if request.method == "POST": |
|
user_input = int(request.form["user_input"]) |
|
correct_answer = element_outer_electrons[session["element"]] |
|
if user_input == correct_answer: |
|
result = "正解です!" |
|
else: |
|
result = f"残念!正解は {correct_answer} でした。" |
|
return render_template("quiz.html", element=session["element"], result=result) |
|
|
|
return render_template("quiz.html", element=session["element"], result=None) |
|
|
|
@app.route("/next", methods=["POST"]) |
|
def next_question(): |
|
|
|
session["element"] = random.choice(list(element_outer_electrons.keys())) |
|
return redirect(url_for("quiz")) |
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |
|
|