gpt4 book ai didi

python - Monty Hall 问题在切换和不切换时返回 33% 的结果

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:46 24 4
gpt4 key购买 nike

所以我一直在尝试用 Python 来模拟一个类似于 Monty Hall 问题的问题,我预计转换后会产生 66% 的获胜结果,但事实并非如此。有人可以指出我哪里出了问题吗?我试图根据转换来得出获胜和失败的百分比。任何帮助将不胜感激。

import random

#Ask the user for how many runs
runs = int(input("How many games do you want to simulate?"))
switchwins, nonswitchwins, switchlosses, nonswitchlosses = 0, 0, 0, 0

# Get the random number started with a seed
random.seed()

#run once for user switching and once for user not switching
for swap in True,False:
# Do everything for the number of runs we have
for i in range(runs):

# Setup the prize locations
prizes = ['nothing', 'nothing', 'coin']
# Randomly mix them up
random.shuffle(prizes)

#select a random location
ChoiceA = random.randrange(3)

# print("Before the prize is revealed, I will show you what is in one of the other hiding places")

# remove one of the other hiding places which has nothing as a prize and isn't ChoiceA
for currentlocation, contents in enumerate(prizes):
if currentlocation != ChoiceA and contents == "nothing":
showlocation = currentlocation
# print("There is nothing in this location", showlocation)
break


if swap:
#swap to the other location
for currentlocation, contents in enumerate(prizes):
if currentlocation != ChoiceA and contents != showlocation:
swap_to = currentlocation

# check if the swapped choice is a win
if prizes[swap_to] == "coin":
switchwins +=1
else:
switchlosses +=1
# when not swapping locations check for win
else:
if prizes[ChoiceA] == "coin":
nonswitchwins +=1
else:
nonswitchlosses +=1

print("This is the number of wins if the user switched",
round((switchwins/runs)*100,1), "%")

print("This is the number of wins if the user didn't switch",
round((nonswitchwins/runs)*100,1),"%")

最佳答案

你写了if currentlocation != ChoiceA andcontents != showlocation:,但这不是if currentlocation != ChoiceA and currentlocation != showlocation:吗?

关于python - Monty Hall 问题在切换和不切换时返回 33% 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53366172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com