英単語テストを作成中
以下のようにコードを書きました。このままでは小文字しかリストから抜け出せず、問3の本来「December」としたい所が謎の単語を生成してしまっています。なにか解決の手立てをいただければ幸いです。
またおそらく文字が綺麗に抜き出せていない関係で(?)、正答もずれてしまっています。
お力添えいただけないでしょうか。よろしくお願いします。
python3
1import re 2import random 3 4source = 'english_words.txt' 5with open(source) as f: 6 date = f.read() 7 8english_words = re.findall('[a-z]+',date) 9ja = re.findall('\s.*\n',date) 10 11meanings = [] 12for word in ja: 13 m = re.sub('\t|\n','',word) 14 meanings.append(m) 15 16words_dict = dict(zip(english_words,meanings)) 17 18n_tests = 1 19n_questions = 50 20 21for test_num in range(n_tests): 22 with open('英単語テスト_{:02d}.txt'.format(test_num+1),'w') as f: 23 24 f.write('出席番号:\n' 25 '名前:\n\n' 26 '第{}回 英単語テスト\n\n'.format(test_num+1)) 27 28 for question_num in range(n_questions): 29 question_word = random.choice(english_words) 30 correct_answer = words_dict[question_word] 31 32 meanings_copy = meanings.copy() 33 meanings_copy.remove(correct_answer) 34 wrong_answers = random.sample(meanings_copy,3) 35 36 answer_options = [correct_answer] + wrong_answers 37 random.shuffle(answer_options) 38 39 f.write('問{}.{}\n'.format(question_num+1,question_word)) 40 41 for i in range(4): 42 f.write('{}. {}\n'.format(i+1,answer_options[i])) 43 44 f.write('\n\n')
結果 出席番号: 名前: 第1回 英単語テスト 問1.get 1. 到着する 2. 待つ 3. 3月 4. 少女 問2.weather 1. 私たちは 2. 9 3. 50 4. 道,方法 問3.ecember 1. 12月 2. 去る,残す,置いて行く 3. 春 4. 右,権利 問4.nobody 1. それ自身 2. だれも~ない 3. ~もまた,~すぎる 4. 雨 ・・・以下50問まで続く
試した事
lower()を使いたいのですが、名詞等どうしても大文字のままにしておきたい単語もあるので、使えません。
補足
python3.6.6
Jupiter notebook
words_dictはこんな感じになります。 print(words_dict) {'a': '1つの', 'able': 'できる,有能な', 'afternoon': '午後', 'again': '再び', 'age': '年齢,時代', 'ago': '前', 'all': 'すべての', 'already': 'すでに', 'also': 'もまた', 'always': 'いつも', 'merica': 'アメリカ合衆国', 'an': '1つの', 'and': '~と、そして', 'angry': '怒った', 'animal': '動物', 'apple': 'りんご', 'pril': '4月', 'arrive': '到着する', 'at': '~に', 'ugust': '8月', 'aunt': 'おば', 'ustralia': 'オーストラリア', 'autumn': '秋', 'bad': '悪い', 'bag': 'カバン', 'ball': 'ボール', 'bank': ' 土手', 'baseball': '野球', 'basketball': 'バスケットボール', 'bat': 'バット', 'beautiful': '美しい', 'bed': 'ベッド', 'best': '一番良い', 'big': '大きい', 'bike': '自転車', 'bird': '鳥', 'birthday': '誕生日', 'black': '黒い', 'blue': '青い', 'boat': 'ボート', 'book': '本', 'both': '両方', 'box': '箱', 'boy': '少年', 'bread': 'パン', 'breakfast': '朝食', 'brother': '兄弟', 'bus': 'バス', 'busy': '忙しい', 'but': 'しかし', 'buy': '買う', 'by': '~のそばに、~よって', 'cake': 'ケーキ', 'camera': 'カメラ', 'can': '~できる、してもよい', 'anada': 'カナダ', ・・・と続いていきます。
回答2件
あなたの回答
tips
プレビュー