蒙蒂霍尔 - 没有跑

时间:2014-11-18 14:16:41

标签: python

我不确定我的代码有什么问题,我需要一些帮助。当我尝试运行我的程序时,它会在我的第一个旁边显示无效语法,如果我认为我可能是缩进错误但没有任何工作。

这是我的代码:

import random

def montyHall():
    car = random.randint(1,3)
    guess1 = random.randint(1,3)
    for i in range(1,3):
        if ((not(i == car) and not(i == guess1)):
            return i

    newGuess = not(i) and not(guess1)
    if (newGuess == car):
        stay = True
    elif (guess1 == car):
        switch = False
    return strategyOne

NUM_OF_TRIALS = 1000
stay = 0
switch = 0

for i in range(NUM_OF_TRIALS):
    if(montyHall()):
        stay += 1
    else:
        switch += 1
print("Staying wins", stay/NUM_OF_TRIALS, "% of the time")
print("Switching wins", switch/NUM_OF_TRIALS, "% of the time") 

1 个答案:

答案 0 :(得分:2)

对于许多括号,你不需要在python中使用括号。

尝试更改:

if ((not(i == car) and not(i == guess1)):

   if i != car and i != guess1: