playAgain = True while playAgain: program = input("Do you want to play the game (type \"game\" or run multiple trials (type \"trials\")? ") if program == "game": playAgainGame = True print('Behind one door is a new car!\nBehind the other two are goats!\nYou get what you pick!') while playAgainGame: car = randint(1, 3) if car == 1: goatA = 2; goatB = 3 elif car == 2: goatA = 1; goatB = 3 elif car == 3: goatA = 1; goatB = 2 doorChoice = int(input('Choose a door, 1, 2, or 3! ')) if doorChoice == goatA: reveal = goatB if doorChoice == goatB: reveal = goatA if doorChoice == car: randGoat = randint(1, 2) if randGoat == 1: reveal = goatA if randGoat == 2: reveal = goatB print("\nInteresting choice! What if I told you that there's a goat behind Door #%d?" %reveal) print("You can stick with your current choice, or you can change to the other not-yet-open door!\n") newChoice = int(input("Now which door do you want? ")) if newChoice == car: print("\nCongratulations! You won a car!") else: print("\nI hope you like goats!\n") playAgainGame = False if program == "trials": win = 0; loss = 0; trials = 0 tactic = input("Should the program stay or switch? ") trialsTotal = int(input("How many trials should be run? ")) while trials < trialsTotal: trials += 1 car = randint(1, 3) doorChoice = randint(1, 3) if tactic == "switch": if doorChoice != car: doorChoice = car else: doorChoice += 1 if doorChoice == car: win += 1 else: loss += 1 print("\nBy choosing to", tactic, "for %d games, you have won %d cars and %d goats!" % (trialsTotal, win, loss)) print("You won ", 100*win/trialsTotal, "% of the time and lost ", 100*loss/trialsTotal, "% of the time.") if input("Want to run the program again? Yes or no\n") == "no": playAgain = False print("Goodbye!") print()
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