Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

monty hall problem in python

import random
def play_monty_hall(choice):
    # Prize behind the door
    # initial ordering doesn't matter 
    prizes =  ['goat', 'car','goat']

    # Randomizing the prizes
    random.shuffle(prizes) 
    
    # Determining door without car to open
    while True:
        opening_door = random.randrange(len(prizes))
        if prizes[opening_door] != 'car' and choice-1 != opening_door:
            break
    
    opening_door = opening_door + 1
    print('We are opening the door number-%d' % (opening_door))
    
    # Determining switching door
    options = [1,2,3]
    options.remove(choice)
    options.remove(opening_door)
    switching_door = options[0]
    
    # Asking for switching the option
    print('Now, do you want to switch to doornumber-%d? (yes/no)' %(switching_door))
    answer = input()
    if answer == 'yes':
        result = switching_door - 1
    else:
        result = choice - 1
    
    # Displaying the player's prize 
    print('And your prize is ....', prizes[result].upper())
    
# Reading initial choice
choice = int(input('Which door do you want to choose? (1,2,3): '))

# Playing game
play_monty_hall(choice) 


Comment

PREVIOUS NEXT
Code Example
Python :: django model remove duplicates 
Python :: add a new column to numpy array 
Python :: how to stop auto restart flask python 
Python :: how does HTTPServer work in python 
Python :: divide every element in numpy array 
Python :: pyspark add_months 
Python :: what if discord.py python add-in does not work 
Python :: discord.py message user 
Python :: find keys to minimum value in dict 
Python :: mkdir if not exists python 
Python :: K-Means Clustering in Python – 3 clusters 
Python :: read image and resize 
Python :: python regular expressions 
Python :: random letters generator python 
Python :: DJANGO rest framework GET POST 
Python :: how to add createsuper user in django 
Python :: python returned non-zero exit status 1. 
Python :: permutation python 
Python :: console-based animation-simple 
Python :: check if an object has an attribute in Python 
Python :: get char of string python 
Python :: generate random list and find max in list python 
Python :: type checking python 
Python :: css selenium 
Python :: print out session information django 
Python :: dask read csv 
Python :: mkvirtualenv python version 
Python :: Genisim python 
Python :: odoo scaffold command 
Python :: python typecast 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =