プロンプト
import random
def monty_hall_game(change_choice):
doors = ['car', 'goat', 'goat']
random.shuffle(doors) # ドアの配置をランダムに並び替える
player_choice = random.randint(0, 2) # プレーヤーが最初に選ぶドアのインデックス
# ホストが1つのドアを選び、それを開ける
while True:
host_choice = random.randint(0, 2)
if host_choice != player_choice and doors[host_choice] == 'goat':
break
if change_choice:
# プレーヤーが選んだドアを変更する場合
remaining_door = 3 - player_choice - host_choice # 選んだドアとホストが選んだドア以外のドアのインデックス
player_choice = remaining_door
# 結果を判定
if doors[player_choice] == 'car':
return 'win'
else:
return 'lose'
# ゲームを実行して結果を表示
num_games = 1000 # ゲームの実行回数
change_choice = True # プレーヤーがドアを変更するかどうか
wins = 0
for _ in range(num_games):
result = monty_hall_game(change_choice)
if result == 'win':
wins += 1
win_percentage = (wins / num_games) * 100
print(f"ゲーム回数: {num_games}")
print(f"勝利回数: {wins}")
print(f"勝率: {win_percentage}%")