File size: 1,510 Bytes
c2e9464
ccc5076
8b41da9
 
c2e9464
9deadaf
ccc5076
 
 
 
 
 
8651983
ccc5076
 
 
b265f3c
9deadaf
e2daa05
ccc5076
 
8b41da9
9deadaf
ccc5076
9deadaf
 
ccc5076
9deadaf
 
ccc5076
9deadaf
ccc5076
8b41da9
9deadaf
e2daa05
ccc5076
 
272fa53
c2e9464
9deadaf
c2e9464
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
36
37
38
39
40
41
from flask import Flask, render_template, request, redirect, url_for, session
import random

app = Flask(__name__)
app.secret_key = 'your_secret_key_here'  # セッションの安全な署名に必要なキー

questions = {
    "What is the capital of France?": "Paris",
    "What is the largest ocean?": "Pacific",
    "Which planet is known as the Red Planet?": "Mars",
    "What is the chemical symbol for water?": "H2O",
}

def get_random_question():
    question = random.choice(list(questions.keys()))
    return question, questions[question]

@app.route('/', methods=['GET', 'POST'])
def quiz():
    if 'current_question' not in session:  # セッションに現在の問題がない場合は新しいものを取得
        session['current_question'], session['current_answer'] = get_random_question()
    
    result = None

    if request.method == 'POST':
        user_input = request.form['user_input']
        if user_input == session['current_answer']:
            result = '正解です!'
        else:
            result = '不正解です。正解は{}です。'.format(session['current_answer'])
    
    return render_template('quiz.html', question=session['current_question'], result=result)

@app.route('/next', methods=['POST'])
def next_question():
    session.pop('current_question')  # 現在の問題をセッションから削除
    session.pop('current_answer')
    return redirect(url_for('quiz'))

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