Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,34 @@
|
|
1 |
-
from flask import Flask, render_template, request,
|
2 |
import random
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
-
app.secret_key =
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
"
|
12 |
-
}
|
13 |
-
|
14 |
-
def get_random_question():
|
15 |
-
question = random.choice(list(questions.keys()))
|
16 |
-
return question, questions[question]
|
17 |
|
18 |
@app.route('/', methods=['GET', 'POST'])
|
19 |
def quiz():
|
20 |
-
if '
|
21 |
-
session['
|
22 |
-
|
23 |
-
result = None
|
24 |
|
25 |
if request.method == 'POST':
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
else:
|
30 |
-
result =
|
31 |
-
|
32 |
-
return render_template('quiz.html',
|
33 |
|
34 |
@app.route('/next', methods=['POST'])
|
35 |
def next_question():
|
36 |
-
session
|
37 |
-
|
38 |
-
return redirect(url_for('quiz'))
|
39 |
|
40 |
if __name__ == '__main__':
|
41 |
-
app.run(debug=True,
|
|
|
1 |
+
from flask import Flask, render_template, request, session
|
2 |
import random
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
+
app.secret_key = "your_secret_key" # セッションを使用するための秘密鍵を設定します
|
6 |
|
7 |
+
def generate_question():
|
8 |
+
num1 = random.randint(100, 999)
|
9 |
+
num2 = random.randint(100, 999)
|
10 |
+
session['answer'] = num1 + num2
|
11 |
+
return f"{num1} + {num2}"
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
@app.route('/', methods=['GET', 'POST'])
|
14 |
def quiz():
|
15 |
+
if 'answer' not in session:
|
16 |
+
session['question'] = generate_question()
|
|
|
|
|
17 |
|
18 |
if request.method == 'POST':
|
19 |
+
user_answer = int(request.form['user_input'])
|
20 |
+
correct_answer = session.pop('answer', None)
|
21 |
+
if user_answer == correct_answer:
|
22 |
+
result = "正解です!"
|
23 |
else:
|
24 |
+
result = "不正解です。正解は{}です。".format(correct_answer)
|
25 |
+
return render_template('quiz.html', element=session['question'], result=result)
|
26 |
+
return render_template('quiz.html', element=session['question'], result=None)
|
27 |
|
28 |
@app.route('/next', methods=['POST'])
|
29 |
def next_question():
|
30 |
+
session['question'] = generate_question()
|
31 |
+
return render_template('quiz.html', element=session['question'], result=None)
|
|
|
32 |
|
33 |
if __name__ == '__main__':
|
34 |
+
app.run(debug=True,port=7860, host="0.0.0.0")
|