-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty_hall.py
47 lines (29 loc) · 1.07 KB
/
monty_hall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import random
class Game:
def __init__(self, insist: bool, choice: int = 1):
self.insist = insist
self.win_value = random.randint(1, 3)
self.choice = choice
self.win = self.__play()
@classmethod
def host_options_door(cls, win_value, choice_value):
doors = {1, 2, 3}
options = doors - {win_value, choice_value}
return options.pop()
def __play(self):
if self.choice not in range(1, 4):
raise ValueError("Choice must be in 1, 2 or 3")
opened = self.host_options_door(self.win_value, self.choice)
if not self.insist:
new_choice = {1, 2, 3} - {self.choice, opened}
self.choice = new_choice.pop()
return self.choice == self.win_value
def print_win_rate(total: int, insist: bool):
win = 0
for _ in range(total):
if Game(insist).win:
win += 1
print(f"Win rate if{' not' if not insist else ''} insist = {win * 100 / total}%")
if __name__ == "__main__":
print_win_rate(1000, True)
print_win_rate(1000, False)