OzoneAsai commited on
Commit
b9bffd4
1 Parent(s): eaaee7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -1,41 +1,34 @@
1
- from flask import Flask, render_template, request, redirect, url_for, session
2
  import random
3
 
4
  app = Flask(__name__)
5
- app.secret_key = 'your_secret_key_here' # セッションの安全な署名に必要なキー
6
 
7
- questions = {
8
- "What is the capital of France?": "Paris",
9
- "What is the largest ocean?": "Pacific",
10
- "Which planet is known as the Red Planet?": "Mars",
11
- "What is the chemical symbol for water?": "H2O",
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 'current_question' not in session: # セッションに現在の問題がない場合は新しいものを取得
21
- session['current_question'], session['current_answer'] = get_random_question()
22
-
23
- result = None
24
 
25
  if request.method == 'POST':
26
- user_input = request.form['user_input']
27
- if user_input == session['current_answer']:
28
- result = '正解です!'
 
29
  else:
30
- result = '不正解です。正解は{}です。'.format(session['current_answer'])
31
-
32
- return render_template('quiz.html', question=session['current_question'], result=result)
33
 
34
  @app.route('/next', methods=['POST'])
35
  def next_question():
36
- session.pop('current_question') # 現在の問題をセッションから削除
37
- session.pop('current_answer')
38
- return redirect(url_for('quiz'))
39
 
40
  if __name__ == '__main__':
41
- app.run(debug=True, port=7860, host="0.0.0.0")
 
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")