100-9999 / app.py
OzoneAsai's picture
Update app.py
c30a242 verified
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")