1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Day3. 単語帳クイズアプリ - 勝手にChatGPTチャレンジ (Python)

Posted at

前提

本日のお題


3. 単語帳クイズアプリ

何を作る?
words.csv(英単語, 日本語)からランダムに出題し、正解したかどうかを教えてくれる CLI。

学べること

  • csv モジュールでのファイル読み書き
  • リスト / 辞書の扱い
  • ランダム出題ロジック

面白いところ

  • 英単語・専門用語・社内略語など、学びたいものを何でも突っ込める
  • 正答率を記録して「苦手単語だけ出題」など工夫できる

回答

コード

03_words.csv
apple,りんご
banana,バナナ
lemon,レモン
book,本
clock,時計
03_word_quiz.py
import csv
import random

CSV_PATH = "03_words.csv"

def read_csv(path):
    words_list = []

    with open(path, encoding="utf-8") as f:
        reader = csv.reader(f)
        for row in reader:
            words_list.append(row)
    return words_list

def main():
    words_list = read_csv(CSV_PATH)

    e_word, j_word = random.choice(words_list)
    
    input_word = input(f"{e_word} は日本語で何ですか?: ")
    if input_word == j_word:
        print("正解")
    else:
        print("不正解")


if __name__ == "__main__":
    main()
    

実行例

$python 03_word_quiz.py
banana は日本語で何ですか?: a
不正解

$python 03_word_quiz.py
book は日本語で何ですか?: 本
正解

感想

  • CSVの読み込みは普段はpandasで適当に読み込むことが多いがcsv.reader経由で読むのは少し面倒かもと感じた
  • ユーザーがqを入力するまで問題を出し続けたり、現在の正答率とか出せるとより良いと感じる(面白いところのような話)

明日も頑張る

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?