OzoneAsai commited on
Commit
6194752
1 Parent(s): 65defc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +370 -143
app.py CHANGED
@@ -1,157 +1,384 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from flask import Flask, render_template, request, redirect, url_for, session
2
  import random
3
- import sympy as sp
4
 
5
  app = Flask(__name__)
6
- app.secret_key = 'your_secret_key'
7
-
8
- # Define symbols and their descriptions
9
- symbols_info = {
10
- 'solute_mass': '溶質の質量 (g)',
11
- 'solution_mass': '溶液の質量 (g)',
12
- 'moles_of_solute': '溶質のモル数 (mol)',
13
- 'volume_of_solution': '溶液の体積 (L)',
14
- 'mass': '質量 (g)',
15
- 'volume': '体積 (mL)',
16
- 'density': '密度 (g/mL)',
17
- 'molarity': 'モル濃度 (mol/L)',
18
- 'mass_percent': '質量パーセント濃度 (%)'
19
- }
20
 
21
- solute_mass, solution_mass, moles_of_solute, volume_of_solution, mass, volume, density, molarity, mass_percent = sp.symbols(
22
- 'solute_mass solution_mass moles_of_solute volume_of_solution mass volume density molarity mass_percent')
23
-
24
-
25
- # Calculation functions
26
- def calculate_mass_percent(solute_mass, solution_mass):
27
- return round((solute_mass / solution_mass) * 100, 3)
28
-
29
- def calculate_molarity(moles_of_solute, volume_of_solution):
30
- return round(moles_of_solute / volume_of_solution, 3)
31
-
32
- def calculate_density(mass, volume):
33
- return round(mass / volume, 3)
34
-
35
-
36
- # Utility functions
37
- def is_recurring(decimal_str):
38
- if '.' in decimal_str:
39
- fractional_part = decimal_str.split('.')[1]
40
- for i in range(1, len(fractional_part)):
41
- if fractional_part[:i] == fractional_part[i:2*i]:
42
- return True
43
- return False
44
-
45
- def generate_problem_statement(method_name, param_values):
46
- templates = {
47
- 'calculate_mass_percent': [
48
- "溶質の質量が {solute_mass} g で、溶液の質量が {solution_mass} g の場合、質量パーセント濃度を求めなさい。",
49
- "質量パーセント濃度が {mass_percent}% で、溶液の質量が {solution_mass} g の場合、溶質の質量を求めなさい。",
50
- "質量パーセント濃度が {mass_percent}% で、溶質の質量が {solute_mass} g の場合、溶液の質量を求めなさい。"
51
- ],
52
- 'calculate_molarity': [
53
- "溶質のモル数が {moles_of_solute} mol で、溶液の体積が {volume_of_solution} L の場合、モル濃度を求めなさい。",
54
- "モル濃度が {molarity} mol/L で、溶液の体積が {volume_of_solution} L の場合、溶質のモル数を求めなさい。",
55
- "モル濃度が {molarity} mol/L で、溶質のモル数が {moles_of_solute} mol の場合、溶液の体積を求めなさい。"
56
- ],
57
- 'calculate_density': [
58
- "質量が {mass} g で、体積が {volume} mL の場合、密度を求めなさい。",
59
- "密度が {density} g/mL で、体積が {volume} mL の場合、質量を求めなさい。",
60
- "密度が {density} g/mL で、質量が {mass} g の場合、体積を求めなさい。"
61
- ]
62
- }
63
- template_list = templates.get(method_name, ["問題文の生成に失敗しました。"])
64
- template = random.choice(template_list)
65
- return template.format(**param_values)
66
-
67
- def generate_equation_string(method_name, param_values):
68
- try:
69
- equations = {
70
- 'calculate_mass_percent': f"質量パーセント濃度 = (溶質の質量 / 溶液の質量) * 100\n質量パーセント濃度 = ({param_values['solute_mass']} / {param_values['solution_mass']}) * 100",
71
- 'calculate_molarity': f"モル濃度 = 溶質のモル数 / 溶液の体積\nモル濃度 = {param_values['moles_of_solute']} / {param_values['volume_of_solution']}",
72
- 'calculate_density': f"密度 = 質量 / 体積\n密度 = {param_values['mass']} / {param_values['volume']}",
73
- }
74
- return equations.get(method_name, "公式の生成に失敗しました。")
75
- except KeyError as e:
76
- return f"公式の生成中にエラーが発生しました: {e}"
77
-
78
- def solve_equation(equation, var):
79
- try:
80
- solution = sp.solve(equation, sp.Symbol(var))
81
- return solution
82
- except Exception as e:
83
- return [f"Error: {e}"]
84
-
85
- def generate_problem():
86
- methods = [
87
- ('calculate_mass_percent', ['solute_mass', 'solution_mass', 'mass_percent']),
88
- ('calculate_molarity', ['moles_of_solute', 'volume_of_solution', 'molarity']),
89
- ('calculate_density', ['mass', 'volume', 'density']),
90
- ]
91
-
92
  while True:
93
- method_name, params = random.choice(methods)
94
- param_values = {param: random.randint(1, 100) for param in params}
95
-
96
- missing_param = random.choice(params)
97
- param_values[missing_param] = 'x'
98
-
99
- equation = generate_equation_string(method_name, param_values)
100
- equation_lines = equation.split('\n')
101
-
102
- if len(equation_lines) > 1:
103
- equation = equation_lines[1]
104
- else:
105
- continue
106
-
107
- solution = solve_equation(equation, 'x')
108
-
109
- if solution and all(isinstance(s, (int, float)) for s in solution):
110
- solution_value = round(float(solution[0]), 16)
111
- solution_str = f'{solution_value:.16f}'
112
- if is_recurring(solution_str):
113
- continue
114
- if len(str(solution_value).split('.')[1]) <= 4:
115
- break
 
 
 
 
116
 
117
- param_values[missing_param] = solution_value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- problem_statement = generate_problem_statement(method_name, param_values)
120
- equation_string = generate_equation_string(method_name, param_values)
121
- return method_name, param_values, solution_value, problem_statement, equation_string
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- # Route handlers
125
- @app.route('/', methods=['GET', 'POST'])
126
- def index():
127
- if request.method == 'POST':
128
- try:
129
- user_input = float(request.form['user_input'])
130
- solution = float(session.get('solution'))
131
- correct = abs(user_input - solution) < 1e-3
132
- except (ValueError, TypeError):
133
- correct = False
134
- return render_template('result.html', correct=correct, user_input=user_input, solution=solution, problem_statement=session.get('problem_statement'), equation_string=session.get('equation_string'))
 
135
  else:
136
- method_name, param_values, solution, problem_statement, equation_string = generate_problem()
137
- session['problem_statement'] = problem_statement
138
- session['solution'] = solution
139
- session['method_name'] = method_name
140
- session['param_values'] = param_values
141
- session['equation_string'] = equation_string
142
- return render_template('index.html', method_name=method_name, param_values=param_values, solution=solution, problem_statement=problem_statement, symbols_info=symbols_info)
143
-
144
- @app.route('/retry', methods=['GET', 'POST'])
145
- def retry():
146
- if request.method == 'POST':
147
- return redirect(url_for('index'))
148
- if 'method_name' not in session or 'param_values' not in session or 'solution' not in session or 'problem_statement' not in session or 'equation_string' not in session:
149
- return redirect(url_for('index'))
150
- return render_template('index.html', method_name=session.get('method_name'), param_values=session.get('param_values'), solution=session.get('solution'), problem_statement=session.get('problem_statement'), symbols_info=symbols_info, equation_string=session.get('equation_string'))
151
-
152
- @app.route('/new')
153
- def new_problem():
154
- return redirect(url_for('index'))
155
 
156
  if __name__ == '__main__':
157
  app.run(debug=True, host="0.0.0.0", port=7860)
 
1
+ questions = {
2
+ "MgCl2": 95.0,
3
+ "MgO": 40.0,
4
+ "MgF2": 62.0,
5
+ "MgS": 56.0,
6
+ "Mg(OH)2": 58.0,
7
+ "MgI2": 278.0,
8
+ "CaCl2": 111.0,
9
+ "CaO": 56.0,
10
+ "CaF2": 78.0,
11
+ "CaS": 72.0,
12
+ "Ca(OH)2": 74.0,
13
+ "CaI2": 294.0,
14
+ "ZnCl2": 136.0,
15
+ "ZnO": 81.0,
16
+ "ZnF2": 103.0,
17
+ "ZnS": 97.0,
18
+ "Zn(OH)2": 99.0,
19
+ "ZnI2": 319.0,
20
+ "NaCl": 58.5,
21
+ "Na2O": 62.0,
22
+ "NaF": 42.0,
23
+ "Na2S": 78.0,
24
+ "NaOH": 40.0,
25
+ "NaI": 150.0,
26
+ "KCl": 74.5,
27
+ "K2O": 94.0,
28
+ "KF": 58.0,
29
+ "K2S": 110.0,
30
+ "KOH": 56.0,
31
+ "KI": 166.0,
32
+ "HCl": 36.5,
33
+ "H2O": 18.0,
34
+ "HF": 20.0,
35
+ "H2S": 34.0,
36
+ "HOH": 18.0,
37
+ "HI": 128.0,
38
+ "CuCl": 99.0,
39
+ "Cu2O": 143.0,
40
+ "CuF": 82.5,
41
+ "Cu2S": 159.0,
42
+ "CuOH": 80.5,
43
+ "CuI": 190.5,
44
+ "CuCl2": 134.5,
45
+ "CuO": 79.5,
46
+ "CuF2": 101.5,
47
+ "CuS": 95.5,
48
+ "Cu(OH)2": 97.5,
49
+ "CuI2": 317.5,
50
+ "AgCl": 143.5,
51
+ "Ag2O": 232.0,
52
+ "AgF": 127.0,
53
+ "Ag2S": 248.0,
54
+ "AgOH": 125.0,
55
+ "AgI": 235.0,
56
+ "BaCl2": 208.0,
57
+ "BaO": 153.0,
58
+ "BaF2": 175.0,
59
+ "BaS": 169.0,
60
+ "Ba(OH)2": 171.0,
61
+ "BaI2": 391.0,
62
+ "FeCl2": 127.0,
63
+ "FeO": 72.0,
64
+ "FeF2": 94.0,
65
+ "FeS": 88.0,
66
+ "Fe(OH)2": 90.0,
67
+ "FeI2": 310.0,
68
+ "MnCl2": 126.0,
69
+ "MnO": 71.0,
70
+ "MnF2": 93.0,
71
+ "MnS": 87.0,
72
+ "Mn(OH)2": 89.0,
73
+ "MnI2": 309.0,
74
+ "PbCl2": 278.0,
75
+ "PbO": 223.0,
76
+ "PbF2": 245.0,
77
+ "PbS": 239.0,
78
+ "Pb(OH)2": 241.0,
79
+ "PbI2": 461.0,
80
+ "AlCl3": 133.5,
81
+ "Al2O3": 102.0,
82
+ "AlF3": 84.0,
83
+ "Al2S3": 150.0,
84
+ "Al(OH)3": 78.0,
85
+ "AlI3": 408.0,
86
+ "FeCl3": 162.5,
87
+ "Fe2O3": 160.0,
88
+ "FeF3": 113.0,
89
+ "Fe2S3": 208.0,
90
+ "Fe(OH)3": 107.0,
91
+ "FeI3": 437.0,
92
+ "NH4Cl": 53.5,
93
+ "(NH4)2O": 52.0,
94
+ "NH4F": 37.0,
95
+ "(NH4)2S": 68.0,
96
+ "NH4OH": 35.0,
97
+ "NH4I": 145.0,
98
+ "MgSO4": 120.0,
99
+ "MgCO3": 84.0,
100
+ "MgC2O4": 112.0,
101
+ "MgCrO4": 140.0,
102
+ "MgCr2O7": 240.0,
103
+ "MgS2O3": 136.0,
104
+ "Mg3(PO4)2": 262.0,
105
+ "CaSO4": 136.0,
106
+ "CaCO3": 100.0,
107
+ "CaC2O4": 128.0,
108
+ "CaCrO4": 156.0,
109
+ "CaCr2O7": 256.0,
110
+ "CaS2O3": 152.0,
111
+ "Ca3(PO4)2": 310.0,
112
+ "ZnSO4": 161.0,
113
+ "ZnCO3": 125.0,
114
+ "ZnC2O4": 153.0,
115
+ "ZnCrO4": 181.0,
116
+ "ZnCr2O": 185.0,
117
+ "ZnS2O3": 177.0,
118
+ "Zn3(PO4)2": 385.0,
119
+ "Na2SO4": 142.0,
120
+ "Na2CO3": 106.0,
121
+ "Na2C2O4": 134.0,
122
+ "Na2CrO4": 162.0,
123
+ "Na2Cr2O7": 262.0,
124
+ "Na2S2O3": 158.0,
125
+ "Na3PO4": 164.0,
126
+ "K2SO4": 174.0,
127
+ "K2CO3": 138.0,
128
+ "K2C2O4": 166.0,
129
+ "K2CrO4": 194.0,
130
+ "K2Cr2O7": 294.0,
131
+ "K2S2O3": 190.0,
132
+ "K3PO4": 212.0,
133
+ "H2SO4": 98.0,
134
+ "H2CO3": 62.0,
135
+ "H2C2O4": 90.0,
136
+ "H2CrO4": 118.0,
137
+ "H2Cr2O7": 218.0,
138
+ "H2S2O3": 114.0,
139
+ "H3PO4": 98.0,
140
+ "Cu2SO4": 223.0,
141
+ "Cu2CO3": 187.0,
142
+ "Cu2C2O4": 215.0,
143
+ "Cu2CrO4": 243.0,
144
+ "Cu2Cr2O7": 343.0,
145
+ "Cu2S2O3": 239.0,
146
+ "Cu3PO4": 285.5,
147
+ "CuSO4": 159.5,
148
+ "CuCO3": 123.5,
149
+ "CuC2O4": 151.5,
150
+ "CuCrO4": 179.5,
151
+ "CuCr2O7": 279.5,
152
+ "CuS2O3": 175.5,
153
+ "Cu3(PO4)2": 380.5,
154
+ "Ag2SO4": 312.0,
155
+ "Ag2CO3": 276.0,
156
+ "Ag2C2O4": 304.0,
157
+ "Ag2CrO4": 332.0,
158
+ "Ag2Cr2O7": 432.0,
159
+ "Ag2S2O3": 328.0,
160
+ "Ag3PO4": 419.0,
161
+ "BaSO4": 233.0,
162
+ "BaCO3": 197.0,
163
+ "BaC2O4": 225.0,
164
+ "BaCrO4": 253.0,
165
+ "BaCr2O7": 353.0,
166
+ "BaS2O3": 249.0,
167
+ "Ba3(PO4)2": 601.0,
168
+ "FeSO4": 152.0,
169
+ "FeCO3": 116.0,
170
+ "FeC2O4": 144.0,
171
+ "FeCrO4": 172.0,
172
+ "FeCr2O7": 272.0,
173
+ "FeS2O3": 168.0,
174
+ "Fe3(PO4)2": 358.0,
175
+ "MnSO4": 151.0,
176
+ "MnCO3": 115.0,
177
+ "MnC2O4": 143.0,
178
+ "MnCrO4": 171.0,
179
+ "MnCr2O)": 175.0,
180
+ "MnS2O3": 167.0,
181
+ "Mn3(PO4)2": 355.0,
182
+ "PbSO4": 303.0,
183
+ "PbCO3": 267.0,
184
+ "PbC2O4": 295.0,
185
+ "PbCrO4": 323.0,
186
+ "PbCr2O7": 423.0,
187
+ "PbS2O3": 319.0,
188
+ "Pb3(PO4)2": 811.0,
189
+ "Al2(SO4)3": 342.0,
190
+ "Al2(CO3)3": 234.0,
191
+ "Al2(C2O4)3": 318.0,
192
+ "Al2(CrO4)3": 402.0,
193
+ "Al2(Cr2O7)3": 702.0,
194
+ "Al2(S2O3)3": 390.0,
195
+ "AlPO4": 122.0,
196
+ "Fe2(SO4)3": 400.0,
197
+ "Fe2(CO3)3": 292.0,
198
+ "Fe2(C2O4)3": 376.0,
199
+ "Fe2(CrO4)3": 460.0,
200
+ "Fe2(Cr2O7)3": 760.0,
201
+ "Fe2(S2O3)3": 448.0,
202
+ "FePO4": 151.0,
203
+ "Mg(NO3)2": 148.0,
204
+ "(CH3COO)2Mg": 142.0,
205
+ "Mg(MnO4)2": 262.0,
206
+ "Ca(NO3)2": 164.0,
207
+ "(CH3COO)2Ca": 158.0,
208
+ "Ca(MnO4)2": 278.0,
209
+ "Zn(NO3)2": 189.0,
210
+ "(CH3COO)2Zn": 183.0,
211
+ "Zn(MnO4)2": 303.0,
212
+ "NaNO3": 85.0,
213
+ "CH3COONa": 82.0,
214
+ "Na(MnO4)": 142.0,
215
+ "KNO3": 101.0,
216
+ "CH3COOK": 98.0,
217
+ "K(MnO4)": 158.0,
218
+ "HNO3": 63.0,
219
+ "CH3COOH": 60.0,
220
+ "HMnO4": 120.0,
221
+ "CuNO3": 125.5,
222
+ "CH3COOCu": 122.5,
223
+ "CuMnO4": 182.5,
224
+ "Cu(NO3)2": 187.5,
225
+ "(CH3COO)2Cu": 181.5,
226
+ "Cu(MnO4)2": 301.5,
227
+ "AgNO3": 170.0,
228
+ "AgCH3COO": 167.0,
229
+ "AgMnO4": 227.0,
230
+ "Ba(NO3)2": 261.0,
231
+ "Ba(CH3COO)2": 255.0,
232
+ "Ba(MnO4)2": 375.0,
233
+ "Fe(NO3)2": 180.0,
234
+ "Fe(CH3COO)2": 174.0,
235
+ "Fe(MnO4)2": 294.0,
236
+ "Mn(NO3)2": 179.0,
237
+ "Mn(CH3COO)2": 173.0,
238
+ "MnNO3": 117.0,
239
+ "MnCH3COO": 114.0,
240
+ "Mn(MnO4)2": 293.0,
241
+ "Pb(NO3)2": 331.0,
242
+ "Pb(CH3COO)2": 325.0,
243
+ "Pb(MnO4)2": 445.0,
244
+ "Al(NO3)3": 213.0,
245
+ "Al(CH3COO)3": 204.0,
246
+ "Al(MnO4)3": 384.0,
247
+ "Fe(NO3)3": 242.0,
248
+ "Fe(CH3COO)3": 233.0,
249
+ "Fe(MnO4)3": 413.0,
250
+ "Mn(NO3)3": 241.0,
251
+ "Mn(CH3COO)3": 232.0,
252
+ "Mn(MnO4)3": 412.0,
253
+ "Pb(NO3)3": 393.0,
254
+ "Pb(CH3COO)3": 384.0,
255
+ "Pb(MnO4)3": 564.0,
256
+ "Al(NO3)2": 151.0,
257
+ "Al(CH3COO)2": 145.0,
258
+ "Al(MnO4)2": 265.0,
259
+ }
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
  from flask import Flask, render_template, request, redirect, url_for, session
270
  import random
271
+ import sympy
272
 
273
  app = Flask(__name__)
274
+ app.secret_key = 'your_secret_key_here' # セッションの安全な署名に必要なキー
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
+ def generate_non_repeating_decimal(range_start, range_end, step):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  while True:
278
+ value = round(random.uniform(range_start, range_end), step)
279
+ if value != int(value):
280
+ break
281
+ return value
282
+
283
+ templates = {
284
+ "溶質の質量が {solute_mass} g で、溶液の質量が {solution_mass} g の場合、質量パーセント濃度を求めなさい。": lambda solute_mass, solution_mass: (solute_mass / solution_mass) * 100,
285
+ "質量パーセント濃度が {mass_percent}% で、溶液の質量が {solution_mass} g の場合、溶質の質量を求めなさい。": lambda mass_percent, solution_mass: (mass_percent / 100) * solution_mass,
286
+ "質量パーセント濃度が {mass_percent}% で、溶質の質量が {solute_mass} g の場合、溶液の質量を求めなさい。": lambda mass_percent, solute_mass: solute_mass / (mass_percent / 100),
287
+ "溶質のモル数が {moles_of_solute} mol で、溶液の体積が {volume_of_solution} L の場合、モル濃度を求めなさい。": lambda moles_of_solute, volume_of_solution: moles_of_solute / volume_of_solution,
288
+ "モル濃度が {molarity} mol/L で、溶液の体積が {volume_of_solution} L の場合、溶質のモル数を求めなさい。": lambda molarity, volume_of_solution: molarity * volume_of_solution,
289
+ "モル濃度が {molarity} mol/L で、溶質のモル数が {moles_of_solute} mol の場合、溶液の体積を求めなさい。": lambda molarity, moles_of_solute: moles_of_solute / molarity,
290
+ "質量が {mass} g で、体積が {volume} mL の場合、密度を求めなさい。": lambda mass, volume: mass / volume,
291
+ "密度が {density} g/mL で、体積が {volume} mL の場合、質量を求めなさい。": lambda density, volume: density * volume,
292
+ "密度が {density} g/mL で、質量が {mass} g の場合、体積を求めなさい。": lambda density, mass: mass / density,
293
+ "化学物質 {chemical} のモル質量が {molar_mass} g/mol の場合、 {moles} mol の質量を求めなさい。": lambda molar_mass, moles: molar_mass * moles,
294
+ "15%(1~30のrandom,ステップ=0.1)の酢酸({chemical})水溶液200g(1~10000の乱数,整数)に含まれる酢酸分子の物質量を求めよ。": lambda chemical, molar_mass, concentration, solution_mass: (concentration / 100) * solution_mass / molar_mass,
295
+ "3.8g(1~500の乱数, ステップ=0.1)の塩化マグネシウム({chemical})を水に溶かして100mL(100~10000,step=100)にした溶液のモル濃度を求めよ。": lambda chemical, molar_mass, solute_mass, solution_volume: solute_mass / molar_mass / (solution_volume / 1000),
296
+ "8.0g(乱数,1~100,整数, 個々の変数名を'solute'とする)の水酸化ナトリウム({chemical})を水に溶かして{solvent}gにした水溶液の密度は1.25g((solute+solvent)/solvent)である。この溶液のモル濃度を求めよ。": lambda chemical, molar_mass, solute, solvent: solute / molar_mass / (solute + solvent) * (solute + solvent) / (solute + solvent)
297
+ }
298
+
299
+
300
+
301
+ @app.route('/')
302
+ def index():
303
+ # ランダムなテンプレートを選択
304
+ template = random.choice(list(templates.keys()))
305
 
306
+ # テンプレートに必要な変数を生成
307
+ solute_mass = round(random.uniform(1, 100), 2) if 'solute_mass' in template else None
308
+ solution_mass = round(random.uniform(100, 1000), 2) if 'solution_mass' in template else None
309
+ mass_percent = round(random.uniform(1, 50), 2) if 'mass_percent' in template else None
310
+ moles_of_solute = round(random.uniform(0.1, 10), 2) if 'moles_of_solute' in template else None
311
+ volume_of_solution = round(random.uniform(0.1, 10), 2) if 'volume_of_solution' in template else None
312
+ molarity = round(random.uniform(0.1, 10), 2) if 'molarity' in template else None
313
+ mass = round(random.uniform(1, 1000), 2) if 'mass' in template else None
314
+ volume = round(random.uniform(1, 1000), 2) if 'volume' in template else None
315
+ density = round(random.uniform(0.5, 20), 2) if 'density' in template else None
316
+ chemical = random.choice(list(questions.keys())) if 'chemical' in template else None
317
+ molar_mass = questions[chemical] if chemical else None
318
+ moles = round(random.uniform(0.1, 10), 2) if 'moles' in template else None
319
+ concentration = generate_non_repeating_decimal(1, 30, 1) if 'concentration' in template else None
320
+ solution_mass_2 = random.randint(1, 10000) if 'solution_mass_2' in template else None
321
+ solute = random.randint(1, 100) if 'solute' in template else None
322
+ solvent = solute + random.randint(1, 100) if 'solvent' in template else None
323
 
324
+ # テンプレートに変数を埋め込んで問題を生成
325
+ question = template.format(
326
+ solute_mass=solute_mass,
327
+ solution_mass=solution_mass,
328
+ mass_percent=mass_percent,
329
+ moles_of_solute=moles_of_solute,
330
+ volume_of_solution=volume_of_solution,
331
+ molarity=molarity,
332
+ mass=mass,
333
+ volume=volume,
334
+ density=density,
335
+ chemical=chemical,
336
+ molar_mass=molar_mass,
337
+ moles=moles,
338
+ concentration=concentration,
339
+ solution_mass_2=solution_mass_2,
340
+ solute=solute,
341
+ solvent=solvent
342
+ )
343
 
344
+ # 答えを計算
345
+ answer = None
346
+ if "求めなさい" in template:
347
+ formula = templates[template]
348
+ answer = formula(
349
+ solute_mass,
350
+ solution_mass,
351
+ mass_percent,
352
+ moles_of_solute,
353
+ volume_of_solution,
354
+ molarity,
355
+ mass,
356
+ volume,
357
+ density,
358
+ molar_mass,
359
+ moles,
360
+ concentration,
361
+ solution_mass_2,
362
+ solute,
363
+ solvent
364
+ )
365
 
366
+ session['question'] = question
367
+ session['answer'] = answer
368
+ return render_template('index.html', question=question)
369
+
370
+ @app.route('/check', methods=['POST'])
371
+ def check():
372
+ user_answer = float(request.form['answer'])
373
+ correct_answer = session.get('answer')
374
+ question = session.get('question')
375
+
376
+ if correct_answer is not None and round(user_answer, 2) == round(correct_answer, 2):
377
+ result = '正解です!'
378
  else:
379
+ result = '不正解です。正しい答えは {} です。'.format(correct_answer)
380
+
381
+ return render_template('result.html', result=result, question=question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
 
383
  if __name__ == '__main__':
384
  app.run(debug=True, host="0.0.0.0", port=7860)