Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, session, redirect, url_for | |
| import random | |
| app = Flask(__name__) | |
| app.secret_key = "your_secret_key_here" | |
| element_atomic_numbers = { | |
| 1: ['H', '水素 (Hydrogen)'], | |
| 2: ['He', 'ヘリウム (Helium)'], | |
| 3: ['Li', 'リチウム (Lithium)'], | |
| 4: ['Be', 'ベリリウム (Beryllium)'], | |
| 5: ['B', 'ホウ素 (Boron)'], | |
| 6: ['C', '炭素 (Carbon)'], | |
| 7: ['N', '窒素 (Nitrogen)'], | |
| 8: ['O', '酸素 (Oxygen)'], | |
| 9: ['F', 'フッ素 (Fluorine)'], | |
| 10: ['Na', 'ナトリウム (Sodium)'], | |
| 11: ['Al', 'アルミニウム (Aluminium)'], | |
| 12: ['Si', 'ケイ素 (Silicon)'], | |
| 13: ['P', 'リン (Phosphorus)'], | |
| 14: ['S', '硫黄 (Sulfur)'], | |
| 15: ['Cl', '塩素 (Chlorine)'], | |
| 16: ['K', 'カリウム (Potassium)'], | |
| 17: ['Ti', 'チタン (Titanium)'], | |
| 18: ['Cr', 'クロム (Chromium)'], | |
| 19: ['Mn', 'マンガン (Manganese)'], | |
| 20: ['Fe', '鉄 (Iron)'], | |
| 21: ['Cu', '銅 (Copper)'], | |
| 22: ['Zn', '亜鉛 (Zinc)'], | |
| 23: ['As', 'ヒ素 (Arsenic)'], | |
| 24: ['Se', 'セレン (Selenium)'], | |
| 25: ['Br', '臭素 (Bromine)'], | |
| 26: ['Nb', 'ニオブ (Niobium)'], | |
| 27: ['Mo', 'モリブデン (Molybdenum)'], | |
| 28: ['Ag', '銀 (Silver)'], | |
| 29: ['Sn', 'スズ (Tin)'], | |
| 30: ['Sb', 'アンチモン (Antimony)'] | |
| } | |
| # 首都のデータを辞書に格納する | |
| capitals = { | |
| 'アルバニア': 'ティラナ', | |
| 'アンドラ': 'アンドラ・ラ・ベリャ', | |
| 'オーストリア': 'ウィーン', | |
| 'ベラルーシ': 'ミンスク', | |
| 'ベルギー': 'ブリュッセル', | |
| 'ボスニア・ヘルツェゴビナ': 'サライェヴォ', | |
| 'ブルガリア': 'ソフィア', | |
| 'クロアチア': 'ザグレブ', | |
| 'チェコ': 'プラハ', | |
| 'デンマーク': 'コペンハーゲン', | |
| 'エストニア': 'タリン', | |
| 'フィンランド': 'ヘルシンキ', | |
| 'フランス': 'パリ', | |
| 'ドイツ': 'ベルリン', | |
| 'ギリシャ': 'アテネ', | |
| 'ハンガリー': 'ブダペスト', | |
| 'アイスランド': 'レイキャヴィーク', | |
| 'アイルランド': 'ダブリン', | |
| 'イタリア': 'ローマ', | |
| 'ラトビア': 'リガ', | |
| 'リヒテンシュタイン': 'ファドゥーツ', | |
| 'リトアニア': 'ヴィリニュス', | |
| 'ルクセンブルク': 'ルクセンブルク', | |
| 'マルタ': 'バレッタ', | |
| 'モルドバ': 'キシナウ', | |
| 'モナコ': 'モナコ', | |
| 'モンテネグロ': 'ポドゴリツァ', | |
| 'オランダ': 'アムステルダム', | |
| '北マケドニア': 'スコピエ', | |
| 'ノルウェー': 'オスロ', | |
| 'ポーランド': 'ワルシャワ', | |
| 'ポルトガル': 'リスボン', | |
| 'ルーマニア': 'ブカレスト', | |
| 'ロシア': 'モスクワ', | |
| 'サンマリノ': 'サンマリノ', | |
| 'セルビア': 'ベオグラード', | |
| 'スロバキア': 'ブラチスラヴァ', | |
| 'スロベニア': 'リュブリャナ', | |
| 'スペイン': 'マドリード', | |
| 'スウェーデン': 'ストックホルム', | |
| 'スイス': 'ベルン', | |
| 'ウクライナ': 'キーウ', | |
| 'イギリス': 'ロンドン', | |
| 'バチカン': 'バチカン', | |
| 'アルメニア': 'エレバン', | |
| 'アゼルバイジャン': 'バクー', | |
| 'キプロス': 'ニコシア', | |
| 'ジョージア': 'トビリシ', | |
| 'イスラエル': 'エルサレム', | |
| 'カザフスタン': 'アスタナ', | |
| 'トルコ': 'アンカラ', | |
| "エチオピア": "アディスアベバ", | |
| } | |
| element_outer_electrons: dict[str, int] = { | |
| "水素": 1, | |
| "ヘリウム": 2, | |
| "リチウム": 1, | |
| "ベリリウム": 2, | |
| "ホウ素": 3, | |
| "炭素": 4, | |
| "窒素": 5, | |
| "酸素": 6, | |
| "フッ素": 7, | |
| "アルゴン": 8, | |
| "クリプトン": 8, | |
| "キセノン": 8, | |
| "ナトリウム": 1, | |
| "マグネシウム": 2, | |
| "アルミニウム": 3, | |
| "ケイ素": 4, | |
| "リン": 5, | |
| "硫黄": 6, | |
| "塩素": 7, | |
| "カリウム": 1, | |
| "カルシウム": 2, | |
| # 他の元素も追加可能 | |
| } | |
| # ランダムな国名とその首都を取得する関数 | |
| def get_random_country(): | |
| country = random.choice(list(capitals.keys())) | |
| return country, capitals[country] | |
| # 初期の問題を設定する | |
| current_country, current_capital = get_random_country() | |
| def index(): | |
| return render_template('index.html') | |
| def quiz_capital(): | |
| global current_country, current_capital | |
| result = None | |
| if request.method == 'POST': | |
| user_input = request.form['user_input'] | |
| if user_input == current_capital: | |
| result = '正解です!' | |
| else: | |
| result = '不正解です。正解は{}です。'.format(current_capital) | |
| return render_template('quiz_capital.html', element=current_country, result=result) | |
| def next_question_capital(): | |
| global current_country, current_capital | |
| current_country, current_capital = get_random_country() | |
| return redirect(url_for('quiz_capital')) | |
| def quiz_element(): | |
| # セッションに元素がない場合はランダムに選択 | |
| if "element" not in session: | |
| session["element"] = random.choice(list(element_outer_electrons.keys())) | |
| if request.method == "POST": | |
| user_input = int(request.form["user_input"]) | |
| correct_answer = element_outer_electrons[session["element"]] | |
| if user_input == correct_answer: | |
| result = "正解です!" | |
| else: | |
| result = f"残念!正解は {correct_answer} でした。" | |
| return render_template("quiz_element.html", element=session["element"], result=result) | |
| return render_template("quiz_element.html", element=session["element"], result=None) | |
| def next_question_element(): | |
| # 次の問題をセッションに設定し、リダイレクト | |
| session["element"] = random.choice(list(element_outer_electrons.keys())) | |
| return redirect(url_for("quiz_element")) | |
| def quiz_element2num(): | |
| # セッションに元素がない場合はランダムに選択 | |
| if "element" not in session: | |
| session["element"] = random.choice(list(element_atomic_numbers.values()))[1] | |
| if request.method == "POST": | |
| user_input = int(request.form["user_input"]) | |
| correct_answer = [key for key, value in element_atomic_numbers.items() if value[1] == session["element"]][0] | |
| if user_input == correct_answer: | |
| result = "正解です!" | |
| else: | |
| result = f"残念!正解は {correct_answer} でした。" | |
| return render_template("quiz_element2num.html", element=session["element"], result=result) | |
| return render_template("quiz_element2num.html", element=session["element"], result=None) | |
| def next_question_element2num(): | |
| # 次の問題をセッションに設定し、リダイレクト | |
| session["element"] = random.choice(list(element_atomic_numbers.values()))[1] | |
| return redirect(url_for("quiz_element2num")) | |
| if __name__ == '__main__': | |
| app.run(debug=True, port=7860,host="0.0.0.0") | |