forked from Pavan-Kamthane/Python_Lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonty-Hall-Python-Simulation.py
47 lines (41 loc) · 1.45 KB
/
Monty-Hall-Python-Simulation.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
from tkinter import StringVar, Label, Tk, Entry
window = Tk()
window.geometry("400x100")
window.title("Monty hall simulation")
window.resizable(0, 0)
same_choice = StringVar()
switched_choice = StringVar()
same_choice.set(0)
switched_choice.set(0)
no_sample = Entry()
Label(text="Same choice").place(x=80, y=8)
Label(text="Switched choice").place(x=80, y=40)
Label(textvariable=same_choice, font=(15)).place(x=180, y=8)
Label(textvariable=switched_choice, font=(15)).place(x=180, y=40)
no_sample.place(x=100, y=70)
def simulate(event):
same_choice_result = 0
switched_choice_result = 0
samples = int(no_sample.get())
doors = ["gold", "goat", "bed"]
for _ in range(samples):
simulated_doors = doors.copy()
random.shuffle(simulated_doors)
first_choice = random.choice(simulated_doors)
simulated_doors.remove(first_choice)
opened_door = (
simulated_doors[0] if simulated_doors[0] != "gold" else simulated_doors[1]
)
simulated_doors.remove(opened_door)
switched_second_choice = simulated_doors[0]
if first_choice == "gold":
same_choice_result += 1
same_choice.set(same_choice_result)
elif switched_second_choice == "gold":
switched_choice_result += 1
switched_choice.set(switched_choice_result)
else:
print("That's will never happed")
no_sample.bind("<Return>", simulate)
window.mainloop()