from flask import Flask, render_template, request, redirect, url_for, session import random import time app = Flask(__name__) app.secret_key = 'your_secret_key_here' # セッションの安全な署名に必要なキー def calc_mass_percent(solute_mass, solution_mass): return (solute_mass / solution_mass) * 100 def calc_solute_mass_from_percent(mass_percent, solution_mass): return (mass_percent / 100) * solution_mass def calc_solution_mass_from_percent(mass_percent, solute_mass): return solute_mass / (mass_percent / 100) def calc_molarity(moles_of_solute, volume_of_solution): return moles_of_solute / volume_of_solution def calc_moles_from_molarity(molarity, volume_of_solution): return molarity * volume_of_solution def calc_volume_from_molarity(molarity, moles_of_solute): return moles_of_solute / molarity def calc_density(mass, volume): return mass / volume def calc_mass_from_density(density, volume): return density * volume def calc_volume_from_density(density, mass): return mass / density def calc_mole_mass(molar_mass, moles): return molar_mass * moles def calc_moles_in_solution(mass_percent, solution_mass, molar_mass): return (mass_percent / 100) * solution_mass / molar_mass def calc_molarity_from_solute(solute_mass, solution_volume, molar_mass): return solute_mass / molar_mass / (solution_volume / 1000) def calc_molarity_from_density(solute, total_mass, density, molar_mass): return solute / molar_mass / (total_mass / density / 1000) templates = { "質量パーセント濃度が {mass_percent}% で、溶液の質量が {solution_mass} g の場合、溶質の質量を求めなさい。": calc_solute_mass_from_percent, "溶質のモル数が {moles_of_solute} mol で、溶液の体積が {volume_of_solution} L の場合、モル濃度を求めなさい。": calc_molarity, "モル濃度が {molarity} mol/L で、溶液の体積が {volume_of_solution} L の場合、溶質のモル数を求めなさい。": calc_moles_from_molarity, "モル濃度が {molarity} mol/L で、溶質のモル数が {moles_of_solute} mol の場合、溶液の体積を求めなさい。": calc_volume_from_molarity, "質量が {mass} g で、体積が {volume} mL の場合、密度を求めなさい。": calc_density, "密度が {density} g/mL で、体積が {volume} mL の場合、質量を求めなさい。": calc_mass_from_density, "密度が {density} g/mL で、質量が {mass} g の場合、体積を求めなさい。": calc_volume_from_density, "化学物質 {chemical} のモル質量が {molar_mass} g/mol の場合、 {moles} mol の質量を求めなさい。": calc_mole_mass, "質量パーセント濃度が {mass_percent}% の {chemical} 水溶液 {solution_mass} g に含まれる {chemical} 分子の物質量を求めなさい。": calc_moles_in_solution, "質量 {solute_mass} g の {chemical} を水に溶かして {solution_volume} mL にした溶液のモル濃度を求めなさい。": calc_molarity_from_solute, "質量 {solute} g の {chemical} を水に溶かして {total_mass} g にした水溶液の密度は {density} g/mL である。この溶液のモル濃度を求めなさい。": calc_molarity_from_density } questions = { "MgCl2": 95.0, "MgO": 40.0, "MgF2": 62.0, "MgS": 56.0, "Mg(OH)2": 58.0, "MgI2": 278.0, "CaCl2": 111.0, "CaO": 56.0, "CaF2": 78.0, "CaS": 72.0, "Ca(OH)2": 74.0, "CaI2": 294.0, "ZnCl2": 136.0, "ZnO": 81.0, "ZnF2": 103.0, "ZnS": 97.0, "Zn(OH)2": 99.0, "ZnI2": 319.0, "NaCl": 58.5, "Na2O": 62.0, "NaF": 42.0, "Na2S": 78.0, "NaOH": 40.0, "NaI": 150.0, "KCl": 74.5, "K2O": 94.0, "KF": 58.0, "K2S": 110.0, "KOH": 56.0, "KI": 166.0, "HCl": 36.5, "H2O": 18.0, "HF": 20.0, "H2S": 34.0, "HOH": 18.0, "HI": 128.0, "CuCl": 99.0, "Cu2O": 143.0, "CuF": 82.5, "Cu2S": 159.0, "CuOH": 80.5, "CuI": 190.5, "CuCl2": 134.5, "CuO": 79.5, "CuF2": 101.5, "CuS": 95.5, "Cu(OH)2": 97.5, "CuI2": 317.5, "AgCl": 143.5, "Ag2O": 232.0, "AgF": 127.0, "Ag2S": 248.0, "AgOH": 125.0, "AgI": 235.0, "BaCl2": 208.0, "BaO": 153.0, "BaF2": 175.0, "BaS": 169.0, "Ba(OH)2": 171.0, "BaI2": 391.0, "FeCl2": 127.0, "FeO": 72.0, "FeF2": 94.0, "FeS": 88.0, "Fe(OH)2": 90.0, "FeI2": 310.0, "MnCl2": 126.0, "MnO": 71.0, "MnF2": 93.0, "MnS": 87.0, "Mn(OH)2": 89.0, "MnI2": 309.0, "PbCl2": 278.0, "PbO": 223.0, "PbF2": 245.0, "PbS": 239.0, "Pb(OH)2": 241.0, "PbI2": 461.0, "AlCl3": 133.5, "Al2O3": 102.0, "AlF3": 84.0, "Al2S3": 150.0, "Al(OH)3": 78.0, "AlI3": 408.0, "FeCl3": 162.5, "Fe2O3": 160.0, "FeF3": 113.0, "Fe2S3": 208.0, "Fe(OH)3": 107.0, "FeI3": 437.0, "NH4Cl": 53.5, "(NH4)2O": 52.0, "NH4F": 37.0, "(NH4)2S": 68.0, "NH4OH": 35.0, "NH4I": 145.0, "MgSO4": 120.0, "MgCO3": 84.0, "MgC2O4": 112.0, "MgCrO4": 140.0, "MgCr2O7": 240.0, "MgS2O3": 136.0, "Mg3(PO4)2": 262.0, "CaSO4": 136.0, "CaCO3": 100.0, "CaC2O4": 128.0, "CaCrO4": 156.0, "CaCr2O7": 256.0, "CaS2O3": 152.0, "Ca3(PO4)2": 310.0, "ZnSO4": 161.0, "ZnCO3": 125.0, "ZnC2O4": 153.0, "ZnCrO4": 181.0, "ZnCr2O": 185.0, "ZnS2O3": 177.0, "Zn3(PO4)2": 385.0, "Na2SO4": 142.0, "Na2CO3": 106.0, "Na2C2O4": 134.0, "Na2CrO4": 162.0, "Na2Cr2O7": 262.0, "Na2S2O3": 158.0, "Na3PO4": 164.0, "K2SO4": 174.0, "K2CO3": 138.0, "K2C2O4": 166.0, "K2CrO4": 194.0, "K2Cr2O7": 294.0, "K2S2O3": 190.0, "K3PO4": 212.0, "H2SO4": 98.0, "H2CO3": 62.0, "H2C2O4": 90.0, "H2CrO4": 118.0, "H2Cr2O7": 218.0, "H2S2O3": 114.0, "H3PO4": 98.0, "Cu2SO4": 223.0, "Cu2CO3": 187.0, "Cu2C2O4": 215.0, "Cu2CrO4": 243.0, "Cu2Cr2O7": 343.0, "Cu2S2O3": 239.0, "Cu3PO4": 285.5, "CuSO4": 159.5, "CuCO3": 123.5, "CuC2O4": 151.5, "CuCrO4": 179.5, "CuCr2O7": 279.5, "CuS2O3": 175.5, "Cu3(PO4)2": 380.5, "Ag2SO4": 312.0, "Ag2CO3": 276.0, "Ag2C2O4": 304.0, "Ag2CrO4": 332.0, "Ag2Cr2O7": 432.0, "Ag2S2O3": 328.0, "Ag3PO4": 419.0, "BaSO4": 233.0, "BaCO3": 197.0, "BaC2O4": 225.0, "BaCrO4": 253.0, "BaCr2O7": 353.0, "BaS2O3": 249.0, "Ba3(PO4)2": 601.0, "FeSO4": 152.0, "FeCO3": 116.0, "FeC2O4": 144.0, "FeCrO4": 172.0, "FeCr2O7": 272.0, "FeS2O3": 168.0, "Fe3(PO4)2": 358.0, "MnSO4": 151.0, "MnCO3": 115.0, "MnC2O4": 143.0, "MnCrO4": 171.0, "MnCr2O)": 175.0, "MnS2O3": 167.0, "Mn3(PO4)2": 355.0, "PbSO4": 303.0, "PbCO3": 267.0, "PbC2O4": 295.0, "PbCrO4": 323.0, "PbCr2O7": 423.0, "PbS2O3": 319.0, "Pb3(PO4)2": 811.0, "Al2(SO4)3": 342.0, "Al2(CO3)3": 234.0, "Al2(C2O4)3": 318.0, "Al2(CrO4)3": 402.0, "Al2(Cr2O7)3": 702.0, "Al2(S2O3)3": 390.0, "AlPO4": 122.0, "Fe2(SO4)3": 400.0, "Fe2(CO3)3": 292.0, "Fe2(C2O4)3": 376.0, "Fe2(CrO4)3": 460.0, "Fe2(Cr2O7)3": 760.0, "Fe2(S2O3)3": 448.0, "FePO4": 151.0, "Mg(NO3)2": 148.0, "(CH3COO)2Mg": 142.0, "Mg(MnO4)2": 262.0, "Ca(NO3)2": 164.0, "(CH3COO)2Ca": 158.0, "Ca(MnO4)2": 278.0, "Zn(NO3)2": 189.0, "(CH3COO)2Zn": 183.0, "Zn(MnO4)2": 303.0, "NaNO3": 85.0, "CH3COONa": 82.0, "Na(MnO4)": 142.0, "KNO3": 101.0, "CH3COOK": 98.0, "K(MnO4)": 158.0, "HNO3": 63.0, "CH3COOH": 60.0, "HMnO4": 120.0, "CuNO3": 125.5, "CH3COOCu": 122.5, "CuMnO4": 182.5, "Cu(NO3)2": 187.5, "(CH3COO)2Cu": 181.5, "Cu(MnO4)2": 301.5, "AgNO3": 170.0, "AgCH3COO": 167.0, "AgMnO4": 227.0, "Ba(NO3)2": 261.0, "Ba(CH3COO)2": 255.0, "Ba(MnO4)2": 375.0, "Fe(NO3)2": 180.0, "Fe(CH3COO)2": 174.0, "Fe(MnO4)2": 294.0, "Mn(NO3)2": 179.0, "Mn(CH3COO)2": 173.0, "MnNO3": 117.0, "MnCH3COO": 114.0, "Mn(MnO4)2": 293.0, "Pb(NO3)2": 331.0, "Pb(CH3COO)2": 325.0, "Pb(MnO4)2": 445.0, "Al(NO3)3": 213.0, "Al(CH3COO)3": 204.0, "Al(MnO4)3": 384.0, "Fe(NO3)3": 242.0, "Fe(CH3COO)3": 233.0, "Fe(MnO4)3": 413.0, "Mn(NO3)3": 241.0, "Mn(CH3COO)3": 232.0, "Mn(MnO4)3": 412.0, "Pb(NO3)3": 393.0, "Pb(CH3COO)3": 384.0, "Pb(MnO4)3": 564.0, "Al(NO3)2": 151.0, "Al(CH3COO)2": 145.0, "Al(MnO4)2": 265.0, } @app.route('/') def index(): while True: start_time = time.perf_counter() # ランダムなテンプレートを選択 template = random.choice(list(templates.keys())) # テンプレートに必要な変数を生成 solute_mass = round(random.uniform(1, 100), 2) if 'solute_mass' in template else None solution_mass = random.randint(1, 10000) if 'solution_mass' in template else None mass_percent = round(random.uniform(1, 30), 1) if 'mass_percent' in template else None moles_of_solute = round(random.uniform(0.1, 10), 2) if 'moles_of_solute' in template else None volume_of_solution = random.randint(100, 10000) if 'volume_of_solution' in template else None molarity = round(random.uniform(0.1, 10), 2) if 'molarity' in template else None mass = random.randint(1, 1000) if 'mass' in template else None volume = random.randint(1, 1000) if 'volume' in template else None density = round(random.uniform(0.5, 20), 2) if 'density' in template else None chemical = random.choice(list(questions.keys())) if 'chemical' in template else None molar_mass = questions[chemical] if chemical else None moles = round(random.uniform(0.1, 10), 2) if 'moles' in template else None solute = random.randint(1, 100) if 'solute' in template else None solvent = random.randint(1, 100) if 'solvent' in template else None total_mass = solute + solvent if solute and solvent else None # 循環小数を避けるために計算を試行 while True: # テンプレートに変数を埋め込んで問題を生成 question = template.format( solute_mass=solute_mass, solution_mass=solution_mass, mass_percent=mass_percent, moles_of_solute=moles_of_solute, volume_of_solution=volume_of_solution, molarity=molarity, mass=mass, volume=volume, density=density, chemical=chemical, molar_mass=molar_mass, moles=moles, solute=solute, solvent=solvent, total_mass=total_mass ) # 答えを計算 answer = None if "求めなさい" in template: formula = templates[template] if template == "溶質の質量が {solute_mass} g で、溶液の質量が {solution_mass} g の場合、質量パーセント濃度を求めなさい。": answer = formula(solute_mass, solution_mass) elif template == "質量パーセント濃度が {mass_percent}% で、溶液の質量が {solution_mass} g の場合、溶質の質量を求めなさい。": answer = formula(mass_percent, solution_mass) elif template == "質量パーセント濃度が {mass_percent}% で、溶質の質量が {solute_mass} g の場合、溶液の質量を求めなさい。": answer = formula(mass_percent, solute_mass) elif template == "溶質のモル数が {moles_of_solute} mol で、溶液の体積が {volume_of_solution} L の場合、モル濃度を求めなさい。": answer = formula(moles_of_solute, volume_of_solution) elif template == "モル濃度が {molarity} mol/L で、溶質のモル数が {moles_of_solute} mol の場合、溶液の体積を求めなさい。": answer = formula(molarity, moles_of_solute) elif template == "質量が {mass} g で、体積が {volume} mL の場合、密度を求めなさい。": answer = formula(mass, volume) elif template == "密度が {density} g/mL で、質量が {mass} g の場合、体積を求めなさい。": answer = formula(density, mass) elif template == "質量パーセント濃度が {mass_percent}% の {chemical} 水溶液 {solution_mass} g に含まれる {chemical} 分子の物質量を求めなさい。": answer = formula(mass_percent, solution_mass, molar_mass) elif template == "質量 {solute_mass} g の {chemical} を水に溶かして {solution_volume} mL にした溶液のモル濃度を求めなさい。": answer = formula(solute_mass, solution_volume, molar_mass) elif template == "質量 {solute} g の {chemical} を水に溶かして {total_mass} g にした水溶液の密度は {density} g/mL である。この溶液のモル濃度を求めなさい。": answer = formula(solute, total_mass, density, molar_mass) # 循環小数でないか確認 if answer and isinstance(answer, float) and (answer * 1000 % 1 == 0): break else: # 再生成 solute_mass = round(random.uniform(1, 100), 2) if 'solute_mass' in template else None solution_mass = random.randint(1, 10000) if 'solution_mass' in template else None mass_percent = round(random.uniform(1, 30), 1) if 'mass_percent' in template else None moles_of_solute = round(random.uniform(0.1, 10), 2) if 'moles_of_solute' in template else None volume_of_solution = random.randint(100, 10000) if 'volume_of_solution' in template else None molarity = round(random.uniform(0.1, 10), 2) if 'molarity' in template else None mass = random.randint(1, 1000) if 'mass' in template else None volume = random.randint(1, 1000) if 'volume' in template else None density = round(random.uniform(0.5, 20), 2) if 'density' in template else None chemical = random.choice(list(questions.keys())) if 'chemical' in template else None molar_mass = questions[chemical] if chemical else None moles = round(random.uniform(0.1, 10), 2) if 'moles' in template else None solute = random.randint(1, 100) if 'solute' in template else None solvent = random.randint(1, 100) if 'solvent' in template else None total_mass = solute + solvent if solute and solvent else None end_time = time.perf_counter() elapsed_time = end_time - start_time print(f"問題生成と答えの計算にかかった時間: {elapsed_time}秒") session['question'] = question session['answer'] = round(answer, 2) return render_template('index.html', question=question) @app.route('/check', methods=['POST']) def check(): user_answer = float(request.form['answer']) correct_answer = session.get('answer') question = session.get('question') if correct_answer is not None and round(user_answer, 2) == round(correct_answer, 2): result = '正解です!' else: result = '不正解です。正しい答えは {} です。'.format(correct_answer) return render_template('result.html', result=result, question=question) if __name__ == '__main__': app.run(debug=True, host="0.0.0.0", port=7860)