蒙蒂霍爾悖論小程式

2021-10-05 06:26:47 字數 3858 閱讀 8727

import tkinter as tk

import random

import tkinter as tk

import random

def start():

print(l2[0])

print(l1)

global win

global win1

win.destroy()

win1.destroy()

for i in range(0,3):

if i != l2[0]-1:

if l1[i] == 0:

del l1[i]

l4[0] = i

print(l1)

break

del l3[l4[0]]

print(l3)

win = tk.tk()

win.title("蒙蒂霍爾悖論遊戲")

win.geometry("300x300")

talk = "我幫你排除了乙個山羊門,"

talk2 = "你還想選擇之前的門嗎?"

h1 = tk.label(win, text=talk, width=50, height=2)

h1.place(x=20, y=200)

h2 = tk.label(win, text=talk2, width=50, height=1)

h2.place(x=20, y=230)

host = tk.label(win, text="ヽ(●'ω'●)ノ:", width=10, height=2)

host.place(x=0, y=200)

win.resizable(false, false)

door1 = door(l3[0])

door1.bind("", func=result1)

door2 = door(l3[1])

door2.bind("", func=result2)

tk.mainloop()

def result1(a):

global win

win.destroy()

b1 = " 號門"

win2 = tk.tk()

win2.geometry("250x250")

win2.title(str(l3[0]) + b1 + "後")

if l1[0] == 1:

talk = "你得到了獎品為車,真厲害"

else:

talk ="你得到的獎品為山羊,還行"

h1 = tk.label(win2, text=talk, width=25, height=2)

h1.place(x=70, y=50)

host = tk.label(win2, text="ヽ(●´ω`●)ノ:", width=10, height=2)

host.place(x=0, y=50)

b1 = tk.button(win2, text="好的owo", width=6, height=2, command=quit)

b1.place(x=125, y=120)

tk.mainloop()

def result2(a):

b1 = " 號門"

win2 = tk.tk()

win2.geometry("250x250")

win2.title(str(l3[1]) + b1 + "後")

if l1[1] == 1:

talk = "你得到了獎品為車,真厲害"

else:

talk = "你得到的獎品為山羊,也還行"

h1 = tk.label(win2, text=talk, width=25, height=2)

h1.place(x=70, y=50)

host = tk.label(win2, text="ヽ(●´ω`●)ノ:", width=10, height=2)

host.place(x=0, y=50)

b1 = tk.button(win2, text="好的owo", width=6, height=2, command=quit)

b1.place(x=125, y=120)

tk.mainloop()

def indoor(a):

global win1

a1 = a.widget["text"]

b1 = " 號門"

for j in a1:

if j in b1:

a1 = a1.replace(j, "")

l2[0] = int(a1)

win1 = tk.tk()

win1.title(str(a1) + b1)

win1.geometry("250x250")

host1 = tk.label(win1, text="(—w—):你真的想選這個門嗎?", width=30, height=2)

host1.place(x=20, y=80)

b1 = tk.button(win1, text="是的", width=6, height=2, command=start)

b1.place(x=40, y=120)

b2 = tk.button(win1, text="我再想想", width=6, height=2, command=win1.destroy)

b2.place(x=120, y=120)

tk.mainloop()

def door(a):

b = " 號門"

door = tk.button(win, text=str(a) + b, width=6, height=7)

door.place(x=10+((a-1)*120), y=40)

return door

win1 = 0

l1 = [0, 0, 0]

l3 = [1, 2, 3]

l2 = [0]

l4 = [0]

a2 = random.randrange(0,3)

l1[a2] = 1

win = tk.tk()

win.title("蒙蒂霍爾悖論遊戲")

win.geometry("300x300")

talk = "門後會有羊或車,請隨意選擇乙個門~"

h1 = tk.label(win, text=talk, width=50, height=2)

h1.place(x=20,y=200)

host = tk.label(win, text="ヽ(●´ω`●)ノ:", width=10, height=2)

host.place(x=0, y=200)

win.resizable(false, false)

door1 = door(1)

door2 = door(2)

door3 = door(3)

door1.bind("", func=indoor)

door2.bind("", func=indoor)

door3.bind("", func=indoor)

tk.mainloop()

效果如下:

模擬蒙蒂霍爾悖論遊戲

題目 假設你正在參加乙個有獎遊戲節目,有3扇門可選,其中一扇門的後面是汽車,另外兩扇門後面是山羊。你選擇了一扇門 比如1號門 後,主持人會開啟後面是山羊的另外一扇門 比如3號門 問你 想改選2號門嗎?然後根據你的選擇確定最終要開啟的門,並確定你獲得山羊 輸 還是汽車 贏 編寫程式,模擬上面的遊戲,要...

三門問題(蒙提霍爾悖論)

蒙提霍爾問題,又稱三門悖論,出自美國的一檔電視節目,問題的描述是這樣的 選手甲面前有三扇門,其中一扇門之後是汽車,其餘兩扇後面是山羊。選手可以選擇三扇門中的任意乙個並且開啟後獲得該扇門後面的東西。當選手選擇了一扇門,但尚未去開啟它的時候,主持人 事先知道每個門之後藏的東西 會在剩下的兩扇門中開啟一扇...

蒙提霍爾問題

蒙提霍爾問題 蒙提霍爾問題,亦稱為蒙特霍問題或三門問題 英文 monty hall problem 是乙個源自博弈論的數學遊戲問題,大致出自美國的電視遊戲節目 let s make a deal。問題的名字來自該節目的主持人蒙提 霍爾 monty hall 這個遊戲的玩法是 參賽者會看見三扇關閉了的...