【发布时间】:2016-01-30 23:19:47
【问题描述】:
我为这个措辞糟糕的问题道歉,我只是想不出更好的方式来表达我的问题。我正在编写一个使用交互式 GUI 演示 Monty Hall 问题的脚本。在我的第一帧中,我有 3 个按钮,分别标记为“A 门”、“B 门”和“C 门”。在这 3 个按钮下方,我还有另外 2 个标记为“切换选择”和“保留选择”的按钮。
代表“门”的 3 个按钮调用我的名为 initialGuess(self,door) 的方法,而底部的 2 个代表用户是否想要保留或切换他的选择的按钮调用方法 switch_choice(self, val)。我想确保用户选择一扇门,以及他是否想在模拟蒙蒂霍尔问题之前切换他的选择。两个按钮运行两种不同的方法,我如何编写一个脚本来确保两种方法都已运行,并且一旦确认用户做出了两种选择,它将运行模拟的主要部分。
为了帮助解决这个问题,这里是我的按钮和相应的方法。 (对于我计划为它们创建其他方法的所有全局变量,我提前道歉,这只是一个开始的蓝图。
class MontyHallSim(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = Label(self, text="Monty Hall Problem", font=Large_Font)
label.place(x=150, y=100, width=500, height=50)
doorA = Button(self, text="Door A", font=Small_Font,
relief=RAISED, height=10, width=50,
command=lambda: self.initialGuess("A"))
doorA.place(x=125, y=200, width=150, height=400)
doorB = Button(self, text="Door B", font=Small_Font,
relief=RAISED, height=10, width=50,
command=lambda: self.initialGuess("B"))
doorB.place(x=325, y=200, width=150, height=400)
doorC = Button(self, text="Door C", font=Small_Font,
relief=RAISED , height=10, width=50,
command=lambda: self.initialGuess("C"))
doorC.place(x=525, y=200, width=150, height=400)
switch= Button(self, text="Switch Choice",
relief=RAISED, height=10, width=50,
command=lambda: self.switch_choice("Y"))
switch.place(x=100, y=600, width=100, height=50)
no_switch = Button(self, text="Keep Choice",
relief=RAISED, height=10, width=50,
command=lambda: self.switch_choice("N"))
no_switch.place(x=600, y=600, width=100, height=50)
global doors
global game
global prize
global guess
global empty_door
global initial
global moderator
global stay
global switch
global selection
selection=None
moderator="Moderator"
guess=None
empty_door="Empty"
prize="Prize"
initial="Guess"
doors=("A", "B", "C")
game={"A":"Empty","B":"Empty", "C":"Empty"}
def initialGuess(self,door):
guess=door
game[guess]=initial
tm.showinfo("Monty Hall", "You Chose Door %.2s" %guess)
return guess
def switch_choice(self,val):
switch="Y"
stay="N"
if val==switch:
selection=switch
tm.showinfo("Monty Hall", "You Chose To Switch Your Guess!")
else:
selection=stay
tm.showinfo("Monty Hall", "You Chose The Original Door")
return selection
def run_sim(self):
iterations=1000
win_count=0
lose_count=0
for _ in range(iterations):
#Sets the Prize stores Prize "Key" in a value for later use
placed=random.choice(doors)
game[placed]=prize
prize_key=list(game.keys())[list(game.values()).index(prize)]
#Sets Moderators Choice
moder=list(game.keys())[list(game.values()).index(empty_door)]
game[moder]=moderator
#checks to see if switch is performed
if selection=="Y":
new_guess=random.choice(
[x for x in doors if x != guess and x!=moderator])
game[new_guess]=initial
game[guess]=empty_door
guess=new_guess
game[prize_key]=prize
else:
pass
#Checks Game dictionary for "Guess"; If guess is present you
#lose...
#If guess is NOT present that means the Prize is in the door that
#you guessed
try:
open_guess = (
list(game.keys())[list(game.values()).index(guess)])
if open_guess in game:
lose_count += 1
except ValueError:
win_count += 1
percent_won=float(win_count/iterations*100.)
percent_lost=float(lose_count/iterations*100)
tm.showinfo("Percent Won: %.2f" %percent_won)
tm.showinfo("Percent Lost: %.2f" %percent_lost)
【问题讨论】:
-
你需要一些带有
True/False的变量来控制它。 -
您可以在类的方法中创建
MontyHallSim的所有全局属性,然后通过self.attribute_name引用。同样的技术将允许您添加额外的变量,如 @furas 建议的,以跟踪用户所做的事情和游戏模拟的状态。然后,您可以添加一个新方法来执行您想要完成的检查并在run_sim()方法之前调用它或从该方法调用它(方法可以调用其他方法)。
标签: python python-3.x button tkinter