Skip to content

Commit 14df7d4

Browse files
authored
Add files via upload
1 parent 47f8add commit 14df7d4

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Monty Hall problem :door:
2+
**Monty's Here! Monty knows everything. :ghost:**\
3+
Monty Hall problem is a probability puzzle in which you challenge yourself to find the car. :car:
4+
5+
## Game Direction
6+
Monty is the game host. He knows everything. He asks you to choose one of the three doors.
7+
Behind two of them, there are two goats but behind one of them, there is your fancy prize which is a car.\
8+
\
9+
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.
10+
\
11+
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! 💯 💯 💯
12+
\
13+
\
14+
![Monty Hall problem](https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Monty_open_door.svg/1200px-Monty_open_door.svg.png)
15+
### Game Tip!
16+
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).
17+
18+
added by: https://github.com/sprshr

0 commit comments

Comments
 (0)