今回の目標
ラッコしか出ない問題の解決
今回の結果
ラッコしか出ない問題の解決
→解決
次回の目標
問題のカテゴリをモンスター別で固定する
(漢字出題をするモンスターが地理の出題をする不自然なところを直す)
csv問題の追加(定期的に)
ラッコしか出ない問題の解決
random.randint関数の第二引数にマジックナンバーが使われていました。
this_question = questions[random.randint(0,2)]
直したのがこれです。
this_question = questions[random.randint(0,len(questions)-1)]
第二引数を、len()関数でタプル数が入るようにしました。
ミスを直すよりも、ミスを見つける方が難しいことが実体験でわかりました。
以下、今回のコードです。
import subprocess import time import csv from playsound import playsound #メモ windows11では古めのバージョンをインストールすること。pip install playsound==1.2.2 import pygame import random import sys import string def get_answer(q):#関数定義 shuffle_kouho=q['kouho'] print(shuffle_kouho) random.shuffle(shuffle_kouho) answer_str=shuffle_kouho.index(q['answer'])#1 return [shuffle_kouho,answer_str] def sf(a1,a2):#success,failuer if a1==chr(65+a2): return 1 else: return 0 # CSVのファイル名を指定して、問題が格納されたリストを返す def csv2list(file_path): questions =[] with open(file_path, 'r',encoding="UTF-8") as f: reader = csv.reader(f) for line in reader:#ここでCSV1行ごとの処理が行われる #print(line) kouho_array = line[1].split('/') #鯏/鯵/鱒/鱚 dict = {'question':line[0],'kouho':kouho_array,'answer':line[2],'category':line[3]} questions.append(dict) questions.pop(0) return questions def battle():#戦闘時の関数定義 i=0 mhp=40 #主人公のHP ehp=50 #敵モンスターのHP pygame.mixer.init(frequency = 44100) bgm_sound = pygame.mixer.Sound("./sound/battle.wav") bgm_sound2 = pygame.mixer.Sound("./sound/quiz1.wav") bgm_sound3 = pygame.mixer.Sound("./sound/quiz2.wav") bgm_sound4 = pygame.mixer.Sound("./sound/madamada.mp3") bgm_sound5 = pygame.mixer.Sound("./sound/kakugo.mp3") bgm_sound.play() #問題のデータベース。ここから問題がピックアップされて出題される。 """ questions =[ {'question':"「洋琴」が表す楽器はどれ?",'kouho':['ピアノ','ギター','チェロ','ハープ'],'answer':"ピアノ",'category':'漢字'}, {'question':"ラッコを漢字で書くとどれ?",'kouho':['海鼠','海獺','海狸','海犬'],'answer':"海獺",'category':'漢字'}, {'question':"ガーナの首都は?",'kouho':['アクラ','アピア','アテネ','アンカラ'],'answer':"アクラ",'category':'地理'}, ]""" questions = csv2list('Dict.csv') this_question = questions[random.randint(0,len(questions)-1)] #questions[0] とかquestions[1]とか # this_question = questions[random.randint(0,2)] #questions[0] とかquestions[1]とか #print(this_question) #sys.exit( ) while i<3: ran = random.randint(1,3) if ran ==2 : bgm_sound5.play() if mhp<=0: bgm_sound4.play() print("主人公は敗北した…") time.sleep(2) break elif ehp<=0: print("モンスターは倒れた!") break else: """ s1="漢字出題モンスターが現れた!" s2=s1.replace(s1,"") print(s1) time.sleep(2) print(s2) """ #没になった。メモリ内容が変わるだけで、画面に出力したのは変わらない。 #print(this_question['category'],end='') #print("出題モンスターが現れた!") #print("漢字出題モンスターが現れた!") print(this_question['category']+"出題モンスターが現れた")#モンスターの名前の表示 time.sleep(2) subprocess.run('CLS',shell=True)#2秒後に消す print(this_question['question']) #print(this_question['kouho']) ans_list=get_answer(this_question) for j in range(0,len(ans_list[0])): print(chr(65+j)+': '+ans_list[0][j])#ASCIIコードで設定しました """ for question_kouho in this_question['kouho']: print('候補: '+question_kouho) """ # ans=input() # if ans=="A": # bgm_sound2.play() # print("モンスターは30のダメージを受けた!") # #bgm_sound.stop() # ehp=ehp-30 # time.sleep(1) # break #ans=input() ans=input()#答えの入力を行わせる if sf(ans,ans_list[1]):#ans==chr(65+ans_list[1]) bgm_sound2.play() print("正解!モンスターは30のダメージを受けた!") #bgm_sound.stop() ehp=ehp-30 time.sleep(1) #break else: bgm_sound3.play() print("主人公は30のダメージを受けた!") mhp=mhp-30 time.sleep(1) i=i+1 time.sleep(2) subprocess.run('CLS',shell=True) battle()#確認用に呼び出し
結論、マジックナンバーは仮に用意しておくための存在である。