molQuiz / outerElectronFl.py
OzoneAsai's picture
Update outerElectronFl.py
ccc5076 verified
raw
history blame
1.51 kB
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")