Pythonゲーム作りの過程 その11

今回の目標

ラッコしか出ない問題の解決

今回の結果

ラッコしか出ない問題の解決
→解決

次回の目標

 問題のカテゴリをモンスター別で固定する
(漢字出題をするモンスターが地理の出題をする不自然なところを直す)
 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()#確認用に呼び出し

結論、マジックナンバーは仮に用意しておくための存在である。

Pythonゲーム作りの過程 その10

今回の目標

csvファイルを引数として読み込み、ディクショナリ型の戻り値として返す関数を作る

今回の結果

→解決

次回の目標

 問題の実行画面でラッコの問題しか出ないのを解決したい

csvファイルを引数として読み込み、ディクショナリ型の戻り値として返す関数を作る

 今回は、自分でできず、教えてもらった過程でなんかコードができてしまいました。心残りです。
 せめて、自分で組み立てらるようにはしたいので自分でわかるようにメモしておきます。
 pop()メソッドは、要素を取り除くときなどに使います。インデックス番号で指定します。

 list名.pop(インデックス番号)

 今のところは、csvモジュールについて手を付けていなかったのでわからないことが多いところです。
 悔しいのですが学校があるのであまり長く書けませんでした。
 あと、実行していくときに指摘されて気づいたのは、ループカウンタのiが事故で問題文表示に使用されていたので、処理が1回で終わるといったことがありました。指摘してもらわなければ、後で困ることになっていました。
 以下、今回のコードです。

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,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()#確認用に呼び出し

Pythonゲーム作りの過程 その9

今回の目標

次回以降のすることをまとめる

今回の結果

→未達成

次回以降のすることをまとめる

 前回以前から、次にすることをまとめることを忘れていました。
 それで遅れを取ったので、実際の作業が後日となりました。

次回目標

 csvを取り扱えるようにする。
 余裕があれば、csvファイルを書きやすくするように調節します。

余談:作業効率を下げない方法

①:次にしたいことをまとめる

 その5以降から、次回目標がかけていませんでした。
 近頃、なぜか作業が遅くなりがちだなと思っていました。  今の状態を振り返ってみると、作業内容を考える時間が上乗せされていました。
 実際には、それで作業に取り掛かる時間が圧迫されていました。

②:作業内容を順序だてて考える(できれば事前に)

 ①の続きにはなりますが、作業の順番をあらかじめ脳内整理しておいたほうがよかったです。
 よくない事例としては、作業内容を考えるだけ考えてそれをどの順序でするのかを考えないことです。
 私はそれ以前の問題でしたが、csvを書き込みやすくする方法を考える前にすることがあります。
 それは、手を動かして問題個所を見つけることです。
 これを見落としていることで、今回は何もできてないのだと思います。
 とにかく、自分の行動を振り返ることが大事ではあります。このために、作業日記を書いたほうがいいと言われたのだと思います。

Pythonゲーム作りの過程 その8

今回の目標

csv形式のファイルのデータ構造を決める

今回の結果

csv形式のファイルのデータ構造を決める
→達成

csv形式のファイルのデータ構造を決める

 今回はデータ構造を考えていました。
 データベースはCSV(Comma Separated Values)にしました。これについては、人間が読みやすいか否かが判断基準でした。ヒューマンリーダブルであるかどうかの話ではあります。
 csvファイルでのデリミタ(区切り文字)はカンマですが、今回はsplit()メソッドを使うことで、カラム名を無駄に作らずに済むようにしています。
 今回は、ファイルを2つ以上にするか、1つでまとめるかの選択をしていました。私は、ノートパソコンの画面の大きさの都合上で結局は見るファイルをこまめに変えないほうを選びました。
 以下が、そのcsvの内容です。

question,kouho,answer,category
「洋琴」が表す楽器はどれ?,ピアノ/ギター/チェロ/ハープ,ピアノ,漢字
ラッコを漢字で書くとどれ?,海鼠/海獺/海狸/海犬,海獺,漢字
ガーナの首都は?,アクラ/アピア/アテネ/アンカラ,アクラ,地理

Pythonゲーム作りの過程 その7

今回の目標

回答の正誤判定を関数化する

今回の結果

回答の正誤判定を関数化する
→達成

回答の正誤判定を関数化する

 今回は、簡単に関数化しておきました。
 戻り値を1か0にすることでif文の条件式のTRUEとFALSEにそれぞれ対応させています。
 以下が実際のコードの一部です。

def sf(a1,a2):#success,failuer
    if a1==chr(65+a2):
        return 1
    else:
        return 0

 次回には、csv形式のデータベースを扱っていきたいと思います。
 これが重そうなので、今回の作業が圧迫されました。
 以下が、今回までのコードです。

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']
    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


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':'地理'},
    ]

    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 i in range(0,len(ans_list[0])):
                print(chr(65+i)+': '+ans_list[0][i])#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()#確認用に呼び出し

コメントは大切

 今のところは困ってはいませんが、数か月後にコードを見直す際にコメントが書いてないと内容を理解するのに時間がかかります。
 振り返ってみると、まだコメントのご利益は今のところは感じていません。
 ですが、数か月後に困るとしたらあったほうがいいと思います。

Pythonゲーム作りの過程 その6

今回の目標

選択肢のシャッフル
回答の正誤判定

今回の結果

選択肢のシャッフル
→達成
回答の正誤判定
→達成
今回は無事に達成できました

選択肢のシャッフル

変数shuffle_kouhoに問題の選択肢のデータを格納し、それをrandomモジュールのshuffle()関数でランダムに入れ替えました。
以下がそのコードです。

shuffle_kouho=q['kouho']
    random.shuffle(shuffle_kouho)

回答の正誤判定

if ans==chr(65+ans_list[1]):

前回に使ったchr()関数を使用し、get_answer関数の戻り値を格納したlist型変数の第二要素を引数の一部として使いました。
以降の目標にはなりますが、関数で正誤判定ができるようにしたいです。
以下、今回までのコードです。

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']
    random.shuffle(shuffle_kouho)
    answer_str=shuffle_kouho.index(q['answer'])#1
    for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])#ASCIIコードで設定しました
    return [shuffle_kouho,answer_str]



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':'地理'},
    ]

    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 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()
        if 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()#確認用に呼び出し

余談:関数の重要性

1年前の自分は自分ではない

ほとんどのコードは自分で作ったものでも忘れます。
コードを振り返る際に、過去の自分の当時考えていたことはもちろん忘れています。
なので、関数は頭の中で整理がしやすくなるので、定義はなるべくすることをお勧めします。
私自身、ソースコードが長くなるにつれて、頭の中でどの処理をまとめているのかを整理するのが大変になります。
関数を定義することで、一連の処理をまとめることでコード内容を頭の中で整理できるようになります。

Pythonゲーム作りの過程 その5

今回の目標

選択肢の正誤判定
選択肢をランダムにする
選択肢A~Dを割り振る
モンスターの名前の設定

今回の結果

選択肢の正誤判定
→手を付けていない
選択肢をランダムにする
→未解決
選択肢A~Dを割り振る
→解決済み
モンスターの名前の設定
→無事解決

今回は、目標の一部を達成できました。

A~Dの割り振りについて

 chr() 関数を以下のように使いました。
※chr()関数はビルトイン関数といい、何もモジュールをインポートしなくても使えます。

for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])#ASCIIコードで設定しました

 ASCIIコードではこのように、65から91で大文字のA~Z,97から123で小文字のa~zに対応しています。

問題のカテゴリからモンスターの名前を変える

 思ったより簡単だったので説明は軽くにします。カテゴリを参照にしてprint関数で表示しただけです。

import random

def get_answer(q):
    shuffle_kouho=q['kouho']
    random.shuffle(shuffle_kouho)
    answer_str=1
    print(q['category']+"出題モンスターが現れた")
    for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])
        print(i)
    return [shuffle_kouho,answer_str]

questions =[
    {'question':"「洋琴」が表す楽器はどれ?",'kouho':['ピアノ','ギター','チェロ','ハープ'],'answer':"ピアノ",'category':'漢字'},
    {'question':"ラッコを漢字で書くとどれ?",'kouho':['海鼠','海獺','海狸','海犬'],'answer':"海獺",'category':'漢字'},
    {'question':"ガーナの首都は?",'kouho':['アクラ','アピア','アテネ','アンカラ'],'answer':"アクラ",'category':'地理'},
]
this_question = questions[random.randint(0,2)] #questions[0] とかquestions[1]とか

print(get_answer(this_question))

 以上になりますが、結果として候補をシャッフルできていませんでした。
 失敗したまま今日は寝ます。以下が現状のコードです。

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']
    random.shuffle(shuffle_kouho)
    answer_str=1
    for i in range(0,len(shuffle_kouho)):
        print(chr(65+i)+': '+shuffle_kouho[i])#ASCIIコードで設定しました
    return [shuffle_kouho,answer_str]

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':'地理'},
]

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'])
        print(get_answer(this_question))
        """
        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()
    if ans=="A":
        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)

参考になったサイト:

https://random-tech-note.jp/alphabet/