from __future__ import division import random #initialize counter variables totalSwitch=0 totalKeep=0 winSwitch=0 winKeep=0 for i in range(1,1000): #pick door where prize is hidden prize=random.randint(1,3); #randomly pick initial guess guess=random.randint(1,3) #initialize list of 3 doors doors=[j for j in range(1,4)] #remove the door that was guessed initially as an option to open doors.remove(guess) if(prize!=guess): #remove the door with the prize in it as an option to open doors.remove(prize) #the remaining door is the one that is does not have a prize and was not picked openDoor=doors[0] #randomly decide to keep the initial guess or switch to the other unopened door: 1-keep, 2-switch keepswitch=random.randint(1,2) if(keepswitch==2): #switch chosen #reset list of doors doors=[j for j in range(1,4)] #remove opened door as option doors.remove(openDoor) #remove intial guess as option doors.remove(guess) #assign remaining door as final guess guess=doors[0] #increment total number of times switched doors totalSwitch+=1 if(guess==prize): #if correct, increment number of times switched and won winSwitch+=1 else: #keep chosen #increment total number of times kept door totalKeep+=1 if(guess==prize): #if correct, increment number of times kept and won winKeep+=1 print("keep: "+str(winKeep/totalKeep)) print("switch: "+str(winSwitch/totalSwitch))
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