File size: 1,260 Bytes
b9bffd4
ccc5076
8b41da9
 
b9bffd4
9deadaf
b9bffd4
c30a242
 
b9bffd4
 
b265f3c
9deadaf
e2daa05
b9bffd4
 
ccc5076
9deadaf
b9bffd4
 
 
 
9deadaf
b9bffd4
 
 
8b41da9
9deadaf
e2daa05
b9bffd4
 
c2e9464
9deadaf
b9bffd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask, render_template, request, session
import random

app = Flask(__name__)
app.secret_key = "your_secret_key"  # セッションを使用するための秘密鍵を設定します

def generate_question():
    num1 = random.randint(100, 9999)
    num2 = random.randint(100, 9999)
    session['answer'] = num1 + num2
    return f"{num1} + {num2}"

@app.route('/', methods=['GET', 'POST'])
def quiz():
    if 'answer' not in session:
        session['question'] = generate_question()

    if request.method == 'POST':
        user_answer = int(request.form['user_input'])
        correct_answer = session.pop('answer', None)
        if user_answer == correct_answer:
            result = "正解です!"
        else:
            result = "不正解です。正解は{}です。".format(correct_answer)
        return render_template('quiz.html', element=session['question'], result=result)
    return render_template('quiz.html', element=session['question'], result=None)

@app.route('/next', methods=['POST'])
def next_question():
    session['question'] = generate_question()
    return render_template('quiz.html', element=session['question'], result=None)

if __name__ == '__main__':
    app.run(debug=True,port=7860, host="0.0.0.0")