Monty Hall模拟器无法正常工作

时间:2019-01-06 22:08:46

标签: python

我试图模拟Monty Hall问题。

首先,我选择了胜出的门并随机选择。然后打开一个不是所选门的门。然后检查是否选择了=胜出的门。如果是这样,我在不切换点(b.num)上加了1。

然后切换选择,我从门列表中删除了选择的门,并将选择的门均等化为列表中的唯一项。然后检查是否再次选择了=获胜门,是否已将其添加到切换点(s.num1)中。

问题在于转换胜率大约等于33。其预期值约为66。问题出在哪里?

import random
from random import randint

def b():
    b.num+=1
b.num=0

def s():
    s.num+=1
s.num=0

d=int(input("try count: "))

def a():
    winner=randint(1,3)
    selection=randint(1,3)
    doors=[1,2,3]
    doors_to_open=randint(1,2)
    if selection == 1:
        if doors_to_open==1:
            doors.remove(2)
        else:
            doors.remove(3)

    if selection == 2:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(3)

    if selection == 3:
        if doors_to_open==1:
            doors.remove(1)
        else:
            doors.remove(2)

    if selection == winner:
        b()
        #print("Not switching win count: ",b.num)

    # switch

    doors.remove(selection)
    selection = doors[0]

    if selection == winner:
        s()
        #print("Switching win count: ",s.num)

for q in range (0,d):
    a()

#print("Switching win count: ",s.num)
print("Switching percent: ",(s.num/d)*100)
#print("Not switching win count: ",b.num)
print("Not switching percent: ",(b.num/d)*100)

input()

2 个答案:

答案 0 :(得分:0)

您在调用s函数时错过了一个不等于运算符。如果添加!在这种情况下。它将解决您的模拟问题。

+----+--------------------+
|freq|collect_list(colour)|
+----+--------------------+
|4   |[green]             |
|7   |[red, yellow, cyan] |
+----+--------------------+

答案 1 :(得分:0)

(感谢Mark Meyer)问题已解决。该代码也打开了胜利之门。这是正确的代码:

import random
from random import randint

def a():
    a.sayı+=1
a.sayı=0

def b():
    b.sayı+=1
b.sayı=0

def c():
    kapılar=[1,2,3]
    cevap=randint(1,3)
    seçim=randint(1,3)

    #kapı aç
    kapılar1=[1,2,3]
    if seçim==cevap:
        kapılar1.remove(cevap)
    else:
            kapılar1.remove(cevap)
            kapılar1.remove(seçim)
    #kapılar1.remove(cevap)
    #kapılar1.remove(seçim)
    kapılar.remove(kapılar1[0])

    #dont switch
    if seçim==cevap:
        a()

    #switch
    kapılar.remove(seçim)
    seçim=kapılar[0]

    if seçim==cevap:
        b()

d=int(input("Deneme sayısı: "))

for e in range(0,d):
    c()

print("Switching win percent: ",(b.sayı/d)*100)
print("Not switching win pecent: ",(a.sayı/d)*100)
input()