# The Monty Hall Problem

I was watching MythBusters and they visited this problem which at Seneca we discussed briefly as well (I might have done this one before but thought I’d repost it anyway):

There are 3 doors, 2 are losers and 1 is a winner You pick a door (2/3 chance it’s a loser) They show you the other loser door (leaving the winner door [1/3 chance] and the loser door [2/3 chance]) You decide to switch your door (since it was 2/3 likely you originally chose a losing door and they took the other losing door away, switching increases your odds since the other door is likely to be the winner door) You win! (still a small chance of losing though) \$ python monty.py 0 100 1('Doors:', {1: 'win', 2: 'lose', 3: 'lose'})('Pick:', 1, 'win')('Show:', 2, 'lose')('Final:', 1, 'win')('Doors:', {1: 'lose', 2: 'lose', 3: 'win'})('Pick:', 2, 'lose')('Show:', 1, 'lose')('Final:', 2, 'lose')('Doors:', {1: 'lose', 2: 'lose', 3: 'win'})('Pick:', 1, 'lose')('Show:', 2, 'lose')('Final:', 1, 'lose')('Wins:', 32, '/', 100, '=', 0.32)\$ python ~/Downloads/monty.py 1 100 1('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})('Pick:', 2, 'win')('Show:', 1, 'lose')('Switching:', 2, 'to', 3)('Final:', 3, 'lose')('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})('Pick:', 3, 'lose')('Show:', 1, 'lose')('Switching:', 3, 'to', 2)('Final:', 2, 'win')('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})('Pick:', 1, 'lose')('Show:', 3, 'lose')('Switching:', 1, 'to', 2)('Final:', 2, 'win')('Wins:', 74, '/', 100, '=', 0.74) import randomimport systry:switch = int(sys.argv[1])except:switch = 0try:rounds = int(sys.argv[2])except:rounds = 0try:debug = int(sys.argv[3])except:debug = 0wins = 0for x in range(0, rounds):doors = ["lose", "lose", "win"]random.shuffle(doors)doors = {1:doors[0], 2:doors[1], 3:doors[2]}if (debug != 0):print("Doors:", doors)pick = random.randint(1, 3)if (debug != 0):print("Pick:", pick, doors[pick])show = 1for k in doors.keys():if ((k != pick) and (doors[k] == "lose")):show = kbreakif (debug != 0):print("Show:", show, doors[show])if (switch != 0):for k in doors.keys():if ((k != pick) and (k != show)):other = kbreakprint("Switching:", pick, "to", other)pick = otherprint("Final:", pick, doors[pick])if (doors[pick] != "lose"):wins += 1if (debug != 0):print("")print("")print("Wins:", wins, "/", rounds, "=", float(wins) / float(rounds))