diff --git a/monty-hall-problem/main.py/README.md b/monty-hall-problem/main.py/README.md new file mode 100644 index 0000000..4c1f40a --- /dev/null +++ b/monty-hall-problem/main.py/README.md @@ -0,0 +1,16 @@ +# Monty Hall problem :door: +**Monty's Here! Monty knows everything. :ghost:**\ +Monty Hall problem is a probability puzzle in which you challenge yourself to find the car. :car: + +## Game Direction +Monty is the game host. He knows everything. He asks you to choose one of the three doors. +Behind two of them, there are two goats but behind one of them, there is your fancy prize which is a car.\ +\ +After you make your first selection, Monty tells you where one of the goats is located. Then he offers you to switch to the other door. You can either stick to your first selection or switch. +\ +Whether you stick to your selection, Monty will open the two other doors. If a goat is behind your door, you lose, otherwise, the car is yours! 💯 💯 💯 +\ +\ +![Monty Hall problem](https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Monty_open_door.svg/1200px-Monty_open_door.svg.png) +### Game Tip! +It is recommended always to switch as there is a higher probability for the car to be behind the door you have not chosen. To understand the probabilities in this game watch [this video on YouTube](https://youtu.be/4Lb-6rxZxx0). diff --git a/monty-hall-problem/main.py/main.py b/monty-hall-problem/main.py/main.py new file mode 100644 index 0000000..ebb12a4 --- /dev/null +++ b/monty-hall-problem/main.py/main.py @@ -0,0 +1,146 @@ +import random +print("Monty Hall Problem") + +def spotDoor(): + #door1 + spotChosen = random.randint(0, 2) + doors[0] = spots[spotChosen] + spots.pop(spotChosen) + #door2 + spotChosen = random.randint(0, 1) + doors[1] = spots[spotChosen] + spots.pop(spotChosen) + #door3 + doors[2] = spots[0] + spots.pop(0) + + +def userFirstChoice(): + print(doorsVisual) + global selectedDoor + #input-check + while True: + try: + print("Which door would you like to open?") + selectedDoor = int(input()) + except: + print("Please enter an integer between 1 and 3") + continue + if selectedDoor >= 1 and selectedDoor <= 3: + break + else: + print("Please enter an integer between 1 and 3") + continue + selectedDoor -= 1 +#reveal the one of the goats + + +def reveal(): + global revealedDoor + revealedDoor = random.randint(0, 2) + while revealedDoor == selectedDoor or doors[revealedDoor] == "CAR": + revealedDoor = random.randint(0, 2) + global doorsVisual + doorsVisual = doorsVisual.replace( + ("| " + str(revealedDoor+1) + " |"), "| GOAT |") + print(doorsVisual) + print("There is a " + doors[revealedDoor] + + " behind door number " + str(revealedDoor + 1)) + print("Would you like to switch? (y/n)") + global switch + switch = str(input()) + while switch != "y" and switch != "n": + print("Please enter y for yes or n for no") + switch = str(input()) + if switch == "y": + switch = True + else: + switch = False + + +def switchingDoor(switch): + if switch: + global switchTo + switchTo = random.randint(0, 2) + while switchTo == revealedDoor or switchTo == selectedDoor: + switchTo = random.randint(0, 2) + print("There is a " + doors[switchTo] + + " behind door number " + str(switchTo+1)) + global doorsVisual + if doors[switchTo] == "CAR": + doorsVisual = doorsVisual.replace( + ("| " + str(switchTo+1) + " |"), "| CAR |") + doorsVisual = doorsVisual.replace( + ("| " + str(selectedDoor+1) + " |"), "| GOAT |") + print(doorsVisual) + print('') + print("You Won!") + else: + doorsVisual = doorsVisual.replace( + ("| " + str(selectedDoor+1) + " |"), "| CAR |") + doorsVisual = doorsVisual.replace( + ("| " + str(switchTo+1) + " |"), "| GOAT |") + print(doorsVisual) + print('') + print("You Lost!") + +#opens selected door while switching door == false + + +def openingDoor(switch): + if not switch: + global notOpened + notOpened = random.randint(0, 2) + while notOpened == revealedDoor or notOpened == selectedDoor: + notOpened = random.randint(0, 2) + print("There is a " + doors[selectedDoor] + + " behind door number " + str(notOpened+1)) + global doorsVisual + if doors[selectedDoor] == "CAR": + doorsVisual = doorsVisual.replace( + ("| " + str(selectedDoor+1) + " |"), "| CAR |") + doorsVisual = doorsVisual.replace( + ("| " + str(notOpened+1) + " |"), "| GOAT |") + print(doorsVisual) + print('') + print("You Won!") + else: + doorsVisual = doorsVisual.replace( + ("| " + str(notOpened+1) + " |"), "| CAR |") + doorsVisual = doorsVisual.replace( + ("| " + str(selectedDoor+1) + " |"), "| GOAT |") + print(doorsVisual) + print('') + print("You Lost!") + + +#Game Loop +while True: + #global vars + spots = ["GOAT", "GOAT", "CAR"] + doors = ["door1", "door2", "door3"] + doorsVisual = """ + |-----------| |-----------| |-----------| + | | | | | | + | | | | | | + | 1 | | 2 | | 3 | + | | | | | | + | | | | | | + |-----------| |-----------| |-----------| + """ + + spotDoor() + userFirstChoice() + reveal() + switchingDoor(switch) + openingDoor(switch) + print("Would you like to play again? (y/n)") + again = input() + while again != "y" and again != "n": + print("Please enter y for yes and n for no") + again = input() + if again == "y": + continue + else: + print("Thanks") + break