Monty Hall Problem

import sys
import random as rnd

#strategy=sys.argv[1]# must be 'stick','choose',or 'switch'

def Solve(strategy):
    wins = 0
    for trail in range(100):
    #The price is always in envelop 0... but we don't know that!
        envelopes = [0,1,2]
        first_chose = rnd.choice(envelopes)
        if first_chose == 0:
            envelopes = [0,rnd.choice([1,2])] #Randomly retain 1 or 2
        else:
            envelopes=[0,first_chose]# Retain winner and first choice
   
        if strategy == 'stick':
            second_choice = first_chose
        elif strategy == 'choose':
            second_choice= rnd.choice(envelopes)
        elif strategy == 'switch':
            envelopes.remove(first_chose)
            second_choice = envelopes[0]     
        if second_choice ==0:
            wins += 1
    print strategy +':' + str( wins)
Solve('stick')
Solve('choose')
Solve('switch')

你可能感兴趣的:(Monty Hall Problem)