|
| 1 | +import random |
| 2 | +print("Monty Hall Problem") |
| 3 | + |
| 4 | +def spotDoor(): |
| 5 | + #door1 |
| 6 | + spotChosen = random.randint(0, 2) |
| 7 | + doors[0] = spots[spotChosen] |
| 8 | + spots.pop(spotChosen) |
| 9 | + #door2 |
| 10 | + spotChosen = random.randint(0, 1) |
| 11 | + doors[1] = spots[spotChosen] |
| 12 | + spots.pop(spotChosen) |
| 13 | + #door3 |
| 14 | + doors[2] = spots[0] |
| 15 | + spots.pop(0) |
| 16 | + |
| 17 | + |
| 18 | +def userFirstChoice(): |
| 19 | + print(doorsVisual) |
| 20 | + global selectedDoor |
| 21 | + #input-check |
| 22 | + while True: |
| 23 | + try: |
| 24 | + print("Which door would you like to open?") |
| 25 | + selectedDoor = int(input()) |
| 26 | + except: |
| 27 | + print("Please enter an integer between 1 and 3") |
| 28 | + continue |
| 29 | + if selectedDoor >= 1 and selectedDoor <= 3: |
| 30 | + break |
| 31 | + else: |
| 32 | + print("Please enter an integer between 1 and 3") |
| 33 | + continue |
| 34 | + selectedDoor -= 1 |
| 35 | +#reveal the one of the goats |
| 36 | + |
| 37 | + |
| 38 | +def reveal(): |
| 39 | + global revealedDoor |
| 40 | + revealedDoor = random.randint(0, 2) |
| 41 | + while revealedDoor == selectedDoor or doors[revealedDoor] == "CAR": |
| 42 | + revealedDoor = random.randint(0, 2) |
| 43 | + global doorsVisual |
| 44 | + doorsVisual = doorsVisual.replace( |
| 45 | + ("| " + str(revealedDoor+1) + " |"), "| GOAT |") |
| 46 | + print(doorsVisual) |
| 47 | + print("There is a " + doors[revealedDoor] + |
| 48 | + " behind door number " + str(revealedDoor + 1)) |
| 49 | + print("Would you like to switch? (y/n)") |
| 50 | + global switch |
| 51 | + switch = str(input()) |
| 52 | + while switch != "y" and switch != "n": |
| 53 | + print("Please enter y for yes or n for no") |
| 54 | + switch = str(input()) |
| 55 | + if switch == "y": |
| 56 | + switch = True |
| 57 | + else: |
| 58 | + switch = False |
| 59 | + |
| 60 | + |
| 61 | +def switchingDoor(switch): |
| 62 | + if switch: |
| 63 | + global switchTo |
| 64 | + switchTo = random.randint(0, 2) |
| 65 | + while switchTo == revealedDoor or switchTo == selectedDoor: |
| 66 | + switchTo = random.randint(0, 2) |
| 67 | + print("There is a " + doors[switchTo] + |
| 68 | + " behind door number " + str(switchTo+1)) |
| 69 | + global doorsVisual |
| 70 | + if doors[switchTo] == "CAR": |
| 71 | + doorsVisual = doorsVisual.replace( |
| 72 | + ("| " + str(switchTo+1) + " |"), "| CAR |") |
| 73 | + doorsVisual = doorsVisual.replace( |
| 74 | + ("| " + str(selectedDoor+1) + " |"), "| GOAT |") |
| 75 | + print(doorsVisual) |
| 76 | + print('') |
| 77 | + print("You Won!") |
| 78 | + else: |
| 79 | + doorsVisual = doorsVisual.replace( |
| 80 | + ("| " + str(selectedDoor+1) + " |"), "| CAR |") |
| 81 | + doorsVisual = doorsVisual.replace( |
| 82 | + ("| " + str(switchTo+1) + " |"), "| GOAT |") |
| 83 | + print(doorsVisual) |
| 84 | + print('') |
| 85 | + print("You Lost!") |
| 86 | + |
| 87 | +#opens selected door while switching door == false |
| 88 | + |
| 89 | + |
| 90 | +def openingDoor(switch): |
| 91 | + if not switch: |
| 92 | + global notOpened |
| 93 | + notOpened = random.randint(0, 2) |
| 94 | + while notOpened == revealedDoor or notOpened == selectedDoor: |
| 95 | + notOpened = random.randint(0, 2) |
| 96 | + print("There is a " + doors[selectedDoor] + |
| 97 | + " behind door number " + str(notOpened+1)) |
| 98 | + global doorsVisual |
| 99 | + if doors[selectedDoor] == "CAR": |
| 100 | + doorsVisual = doorsVisual.replace( |
| 101 | + ("| " + str(selectedDoor+1) + " |"), "| CAR |") |
| 102 | + doorsVisual = doorsVisual.replace( |
| 103 | + ("| " + str(notOpened+1) + " |"), "| GOAT |") |
| 104 | + print(doorsVisual) |
| 105 | + print('') |
| 106 | + print("You Won!") |
| 107 | + else: |
| 108 | + doorsVisual = doorsVisual.replace( |
| 109 | + ("| " + str(notOpened+1) + " |"), "| CAR |") |
| 110 | + doorsVisual = doorsVisual.replace( |
| 111 | + ("| " + str(selectedDoor+1) + " |"), "| GOAT |") |
| 112 | + print(doorsVisual) |
| 113 | + print('') |
| 114 | + print("You Lost!") |
| 115 | + |
| 116 | + |
| 117 | +#Game Loop |
| 118 | +while True: |
| 119 | + #global vars |
| 120 | + spots = ["GOAT", "GOAT", "CAR"] |
| 121 | + doors = ["door1", "door2", "door3"] |
| 122 | + doorsVisual = """ |
| 123 | + |-----------| |-----------| |-----------| |
| 124 | + | | | | | | |
| 125 | + | | | | | | |
| 126 | + | 1 | | 2 | | 3 | |
| 127 | + | | | | | | |
| 128 | + | | | | | | |
| 129 | + |-----------| |-----------| |-----------| |
| 130 | + """ |
| 131 | + |
| 132 | + spotDoor() |
| 133 | + userFirstChoice() |
| 134 | + reveal() |
| 135 | + switchingDoor(switch) |
| 136 | + openingDoor(switch) |
| 137 | + print("Would you like to play again? (y/n)") |
| 138 | + again = input() |
| 139 | + while again != "y" and again != "n": |
| 140 | + print("Please enter y for yes and n for no") |
| 141 | + again = input() |
| 142 | + if again == "y": |
| 143 | + continue |
| 144 | + else: |
| 145 | + print("Thanks") |
| 146 | + break |
0 commit comments