The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
monty hall python
Posted by: brian | June 20, 2010 @ 10:04am
Python Code
[
Download
]
# This program will simulate a sequence from the game show Let's Make A Deal, # which is often called "The Monty Hall Problem" import random import time def displayIntro(): print('Hi, and welcome to Let\'s Make A Deal. I\'m your host, Monty Hall') time.sleep(2) print('Let\'s meet our contestant for today\'s show. What\'s your name?') name = input() print('Great to have you here ' + name) time.sleep(1) print('Tell us a little about yourself') input() print('That\'s fantastic. Let\'s get started with the game') print() time.sleep(2) print('There are three doors next to me. Behind one of these doors is a brand new car! The other two doors have... less glamorous prizes') print() time.sleep(2) print('You get to choose a door and win a prize!') def firstChoice(): print('Which door will you take: Door Number 1, Door Number 2, or Door Number 3?') print('Type 1 2 or 3 to make your choice') choice = input() while choice != '1' and choice != '2' and choice != '3' : print('Take some time, think it over, and make your choice. 1 2 or 3') choice = input() if choice in [1, 2, 3]: choice = int(choice) return choice def MontyOpensDoor(chosenDoor, doorList, carDoor): print('Okay, you\'ve made your choice. Are you certain about that though?') print() time.sleep(2) print('Maybe you\'d like to change your mind.') print() time.sleep(2) print('Tell you what, I\'ll open up one of those other two doors.') print() time.sleep(2) del doorList[chosenDoor - 1] if chosenDoor == carDoor: switch = False montyOpens = random.randint(0,1) openDoor = doorList[montyOpens] del doorList[montyOpens] doorRemaining = doorList[0] else: switch = True montyOpens = random.randint(0,1) if doorList[montyOpens] == carDoor: del doorList[montyOpens] openDoor = doorList[0] else: openDoor = doorList[montyOpens] doorRemaining = carDoor print('Okay, let\'s open up door number ' + openDoor +'...') print() time.sleep(5) print('There\'s a goat behind there!') print() time.sleep(2) print('So would you like to switch to door number' + doorRemaining + ' , or stick it out with door number ' + chosenDoor + '?') return switch def switchDoor(switch): print('Type s to switch') choice = input() if choice == 's': choice = True if choice != 's': choice = False print('Okay, let\'s see what you\'ve won...') print() time.sleep(2) print() time.sleep(2) if switch == choice: print('Congratulations, you\'ve won a new car!') print('Thanks for playing') if switch != choice: print('Oooh, too bad. You\'ve won a goat. I guess you can let it keep your lawn trimmed?') print('Thanks for playing') playAgain = 'yes' displayIntro() while playAgain: door = [1, 2, 3] choice = firstChoice() carDoor = random.randint(1,3) switchResult = MontyOpensDoor(choice, door, carDoor) switchDoor(switchResult) time.sleep(3) print() print('Would you like to play again?') playAgain = input().lower().startswith('y')
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.31 / 8
39 Votes
http://codebin.yi.org/706
page generated in 0.00 seconds