Skip to content

Commit f45d658

Browse files
committed
+ ‘MontyHall.py’
1 parent 8fa64ad commit f45d658

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

MontyHall.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/ python2
2+
import random
3+
curtains = ['A','B','C']
4+
NumOfWinsIfKeep = 0
5+
NumOfWinsIfChange = 0
6+
#10000 Tests to convince us
7+
for x in range (0,10000):
8+
# phase 1
9+
Ferrari = random.choice(curtains)
10+
PlayerChoice = random.choice(curtains)
11+
#host will not open the player's choice on the phase two
12+
curtains.remove(PlayerChoice)
13+
# ... or the Ferrari....
14+
Discard = random.choice(curtains)
15+
while Discard == Ferrari:
16+
Discard = random.choice(curtains)
17+
#open one curtain:
18+
curtains.remove(Discard)
19+
#phase two
20+
RemainingCurtain = curtains[0]
21+
if RemainingCurtain == Ferrari:
22+
# if the player changes his choice, he wins
23+
NumOfWinsIfChange += 1
24+
else :
25+
# if the player keeps his choice, he wins
26+
NumOfWinsIfKeep += 1
27+
#repeat
28+
curtains = ['A','B','C']
29+
print 'In 10000 Tests: \nIf player keeps his choice, he wins '+str(NumOfWinsIfKeep) + ' times('+str(float(NumOfWinsIfKeep)/float(100))+'%)\nIf he changes his option he wins: '+ str(NumOfWinsIfChange)+' times('+str(float(NumOfWinsIfChange)/float(100))+'%)'

0 commit comments

Comments
 (0)