# Monty Hall Problem # Coded by Roland Orre import random stayWins = 0 switchWins = 0 Total=0 doors = range(0,3) for game in xrange(10000): car = random.choice(doors) me = random.choice(doors) selection=list(set(doors)-set([me,car])) monty=random.choice(selection) other=set(doors)-set([me,monty]) #print("car=%s, me=%s, monty=%s, other=%s"%(car,me,monty,other)) if me == car: stayWins += 1 elif car in other: switchWins += 1 #fi Total+=1.0 #rof print("stayWins=%s,switchWins=%s"%(stayWins,switchWins)) print("stayOdds=%s,switchOdds=%s"%(stayWins/Total,switchWins/Total))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question