見出し画像

【9行】pythonで作るシンプルなクイズゲーム<問題はカスタマイズ可能>&解説


コード


pythonには辞書というものがあることは皆さんご存じかと思いますが、
今回、辞書を使った簡単に作れ、カスタマイズできるクイズを作ってみました。コードはこちらです ↓

import random
while True:
    A={"一つ目の問題":"一つ目の問題の答え","二つ目の問題":,"二つ目の問題の答え"}
    B=random.choice(list(A.keys()))
    C=input(f"{B}   >")
    if A[B]==C:
        print("正解")
    else:
        print(A[B])

簡単なコードですが、もしものために解説していきます。

解説

import random

ここでランダム関数をインポートします

while True:

これにより無限ループを作ります

 A={"一つ目の問題":"一つ目の問題の答え","二つ目の問題":,"二つ目の問題の答え"}

これが辞書で、問題と答えを辞書に入れていき、作った辞書を変数Aに代入します

 B=random.choice(list(A.keys()))

ここではrandomからchoice()を呼び出します。random.choice()の引数でlist()を呼び出します。そして、list()の引数で、辞書からkeys()を呼び出します。
これによってランダムな辞書のキーつまりクイズの問題を選び、変数Bに代入できる

 C=input(f"{B}   >")

ここで、プレイヤーにさっきランダムに選んだ問題をだし、プレイヤーの解答を変数Cに代入する

    if A[B]==C:
        print("正解")
    else:
        print(A[B])

ここで問題の答えとプレイヤーの解答が一致するか条件分岐をします
※辞書名[キー名]を入力するとそれは辞書のバリューを示します
もし、一致しているなら正解
一致していないなら答え、つまり辞書のバリューを示します

これで1ループが終了し、次の問題、また次の問題へと無限にランダムで問題が出ていきます。

英単語クイズを作るならの問題と解答集を作ってみました!

{"通る (時が)たつ 合格する":"pass","銀行 土手":"bank","自転車":"bicycle","ラジオ":"radio","デパート":"departmentstore","コンビニエンスストア":"conveniencestore","便利な":"convenient","上着":"jacket","騒音":"noise","騒がしい":"noisy","外側":"outside","内側":"inside","否定文の後で ~もまた":"either","驚き":"surprise","平和":"peace","警察":"police","体験":"experience","部分":"part","地域 面積":"area","畑 野原":"field","空間 宇宙":"space","事実":"fact","欠席して":"absent","十分な":"enough","そのような":"such","~でさえ":"even","私自身":"myself","あなた自身":"yourself","彼女自身":"herself","彼自身":"himself","それ自身":"itself","私たち自身":"ourselves","あなたたち自身":"yourselves","彼ら自身":"themselves","はっきりした 澄んだ":"clear","はっきりと":"clearly","本当の":"real","現実":"reality","村":"village","目が覚める":"wake","より少ない":"less","最も少ない":"least","安い":"cheap","値段":"price","どこでも":"everywhere","否定文で どこにも":"anywhere","どこかに":"somewhere","いつか":"someday","永久に":"forever","違い":"difference","誇り":"pride","誇りに思う":"proud","においがする":"smell","起こる":"happen","説明する":"explain","~を受け取る":"receive","帰る 戻る ~を返す":"return","貸す":"lend","(人)に~させる":"let","標識 看板 合図":"sign","報告 報告書":"report","メニュー":"menu","神経質な 緊張して 心配して":"nervous","寂しい 孤独な":"lonely","一人で":"alone","~をおおう":"cover","~を満たす":"fill","原因":"cause","~を傷つける 痛む":"hurt","生まれる":"born","がっかりした 失望した":"disappointed","事故":"accident","植物":"plant","木材":"wood","木製の":"wooden","ウェブ クモの巣":"web","サイト":"site","歩み 一歩":"step","(ずっと)~している ~したところだ ~したことがある":"have","beの過去分詞":"been","~以来":"since","今までに":"ever","二度":"twice","すでに もう":"already","否定文で まだ 疑問文で もう":"yet","doの過去分詞":"done","~することができる":"able","人間":"human","死んだ":"dead","死":"death","約束する":"promise","~を続ける":"continue","救う 蓄える":"save","驚いた":"surprised","驚くべき":"surprising","危険な":"dangerous","安全な":"safe","可能な":"possible","不可能な":"impossible","必要な":"necessary","努力":"effort","ボート":"boat","船":"ship","恐れて":"afraid","感情":"feeling","生きている":"living","始め":"beginning","目標":"goal","型":"type","健康":"health","健康な":"healthy","理由":"reason","機械":"machine","伝統":"tradition","伝統的な":"traditional","助けになる":"helpful","燃える":"burn","横になる 嘘":"lie","解決する":"solve","同意する":"agree","意見が合わない":"disagree","再生利用する":"recycle","婦人":"lady","道路":"road","国":"nation","国家の":"national","国際的な":"international","最終の":"final","ついに 最後に":"finally","主格 人を先行詞とする":"who","主格 人以外のものを先行詞とする":"which","~を集める":"gather","~を導く 通じる":"lead","~を振る":"shake","撃つ":"shoot","支持":"support","試験":"exam","進路":"course","大学":"college","初歩の":"elementary","席":"seat","どれでも先行詞とする":"that","誰を whoの目的格":"whom","共有する":"share","落ちる":"drop","音楽の":"musical","音楽家":"musician","自然の 当然の":"natural","自然 性質":"nature","命令 注文":"order","~を生産する":"produce","製品":"product","問題 事柄 重要である":"matter","~かしらと思う 不思議に思う":"wonder","線 路線 列":"line","年齢":"age","島":"island","ピクニック":"picnic","昼食時間":"lunchtime","伝達する 伝え合う":"communicate","コミュニケーション":"communication","空気":"air","空港":"airport","飛行機":"airplane","~だけれども":"though","~だけれども thoughより固い表現":"although","上がる 昇る":"rise","~を置く (太陽・月が)沈む":"set","~に所属している":"belong","~の上に":"above","~の下に":"below","~を横切って ~の向こう側に":"across","~のそばに":"beside","~に反対して":"against","~につき":"per","パーセント":"percent","100万":"million","真ん中":"middle","少し":"bit","押す":"push","引く":"pull","~に入る":"enter","入口 入学":"entrance","商売":"business","会社 仲間":"company","パスポート":"passport","観光客":"tourist","訪問者":"visitor","交換":"exchange","主人":"host","ホームステイ":"homestay","故郷":"hometown","準備する":"prepare","~を表現する":"express","表現 表情":"expression","会話":"conversation","議論":"discussion","インタビュー":"interview","意味":"meaning","意見":"opinion","声":"voice","目的":"purpose","結果":"result","場合":"case","過去の":"past","戦う":"fight","戦争":"war","腕":"arm","首":"neck","肩":"shoulder","指":"finger","頭痛":"headache","記憶":"memory","涙 ~を裂く":"tear","注意 注目":"attention","風":"wind","風の強い":"windy","低い":"low","ボウル":"bowl","もも":"peach","パイ":"pie","甘い":"sweet","塩":"salt","ワイン":"wine","バター":"butter","キャンディー":"candy","砂糖":"sugar","広い 幅がある":"wide","誰も~ない":"nobody","の何もない":"none","やあ":"hey","ドラム":"drum","バンド":"band","劇場 映画館":"theater","ステージ":"stage","上演する":"perform","上演者":"performer","上演":"performance","器具 楽器":"instrument","試合":"match","プロ":"professional","薬":"medicine","医学の":"medical","簡単な":"simple","トラ":"tiger","ウマ":"horse","ツル":"crane","獣医":"vet","著者":"author","登場人物":"character","行動":"action","活動的な":"active","勇敢な":"brave","東":"east","西":"west","南":"south","北":"north","跳ぶ":"jump","消える 消滅する":"disappear","喜び":"pleasure","宝物":"treasure","冒険":"adventure","賢い":"clever","美しい すばらしい":"lovely","丸い":"round","東の":"eastern","西の":"western","南の":"southern","北の":"northern","リラックスする":"relax","望む":"wish","習慣":"custom","場所 地点":"spot","像":"statue","屋根":"roof","穴":"hole","手品":"trick","個人の":"personal","毎日の":"daily","前方に":"ahead","特に":"especially","発達する":"develop","宇宙飛行士":"astronaut","持ち主":"owner","笑う":"laugh","笑い 笑い声":"laughter","リスト":"list","技術":"skill","期間 時代":"period","主要な":"major","群衆":"crowd","混みあった":"crowded","厚い":"thick","薄い":"thin","~に頼る ~次第である":"depend","鳴る 輪":"ring","アドバイス":"advice","忠告する":"advise","板":"board","~に乗って":"aboard","状態 体調":"condition","視力 光景":"sight","熱":"fever","病気の":"ill","病気":"illness","森":"forest","草":"grass","成功する":"succeed","成功":"success","成功した":"successful","キャプテン":"captain","チームメイト":"teammate","チームワーク":"teamwork","ベル":"bell","手袋":"glove","通った跡 小道":"track","底":"bottom","文":"sentence","内気な":"shy","太った":"fat","祝う":"celebrate","~を提案する":"suggest","提案":"suggestion","販売":"sale","価格":"cost","コイン":"coin","花びん":"vase","ソファ":"sofa","シャワー":"shower","布":"cloth","タオル":"towel","優れた":"ecellent","快適な":"comfortable","(風が)吹く":"blow","嫌う":"hate","マンガ":"cartoon","工場":"factory","スタッフ":"staff","隣人":"neighbor","正直な":"honest","怠けている":"lazy","公共の":"public","私的な":"private","いくつかの":"several","たくさん 十分":"plenty","とにかく":"anyway","よく考える":"consider","~を描写する":"describe","~を発明する":"invent","役割":"role","英雄":"hero","話し手":"speaker","聴衆":"audience","態度":"attitube","考え 思考":"thought","友情":"friendship","幸福":"happiness","真夜中":"midnight","花火":"firework","議論する":"argue","錠":"lock","マンション":"apartment","エレベーター":"elevator","大陸":"continent","汚染":"pollution","科学の 化学製品 化学物質":"chemical","温度 体温":"temperature","度":"degree","センチメートル":"centimeter","コート":"court","丘":"hill","恐怖":"horror","ポケット":"pocket","詩":"poem","バラ":"rose","石":"stone","地下鉄":"subway","道具":"tool","血":"blood","骨":"bone","脳":"brain","記事":"article","円":"circle","確信して":"confident","公平な":"fair","隠れる ~を隠す":"hide","返事":"reply","封筒":"envelope","形 形式":"form","客 得意先":"customer","記者 レポータ":"reporter","相手 パートナー":"partner","一対 一組":"couple","王子":"prince","王女":"princess","灰色の":"gray","確信して":"certain","間違った":"false"}

これをA=の後にコピペすればいいのです
英単語収録数は数えてないですが文字数でいうと6000字をゆうに超えます
この単語集だけで書くのに三時間かかってしまいました。。。
なにしてんだろ、、是非ご活用ください!


この記事が参加している募集

この記事が気に入ったらサポートをしてみませんか?