Skip to content

Commit 780808d

Browse files
committed
Add doors_left list to keep track of user's choices
When I was playing with the original solution program, I sometimes lost track of which door I had picked and which would be the door I switched to if I decided to switch, so I just added code that told the user exactly that. I also restricted their 2nd choice to be between their original choice or the door that was not revealed to be a goat door. This is because I assume once the goat door is revealed, the user will never choose that door.
1 parent 07395c3 commit 780808d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

1_beginner/chapter6/solutions/monty_hall.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
# Randomize the doors
3131
random.shuffle(doors)
3232

33+
# reset the doors left
34+
doors_left = [1, 2, 3]
35+
3336
# The user enters their 1st choice
3437
print("A new Monty Hall game has begun!")
35-
choice = int(input("Choose from doors 1, 2, or 3... : "))
38+
choice = int(input("Choose from doors 1, 2, or 3...\n> "))
39+
doors_left.remove(choice)
3640

3741
# A door that has a goat is revealed, cannot be the one user chose
3842
# makes a duplicate list to avoid messing up the original
@@ -41,12 +45,19 @@
4145
# removes user's choice so that it won't be opened,
4246
# but keeps in the element to not mess up the indices
4347
reveal_doors[choice - 1] = '-'
44-
45-
print("Monty opens door", reveal_doors.index('G') + 1, "to reveal a goat!")
48+
goat_door = reveal_doors.index('G') + 1
49+
doors_left.remove(goat_door)
50+
print("Monty opens door", goat_door, "to reveal a goat!")
4651

4752
# The user is prompted to choose again
4853
print("With this new information, do you want to switch doors?")
49-
choice = int(input("Choose from doors 1, 2, or 3: "))
54+
print("Your first choice was Door", choice)
55+
print("If you switch, you will be opening Door", doors_left[0])
56+
print("Enter 'y' to switch, or 'n' to keep your first choice.")
57+
switch = input("> ")
58+
59+
if switch == 'y':
60+
choice = doors_left[0]
5061

5162
# The prize behind the user's ultimate choice is revealed!
5263
if doors[choice - 1] == 'C':

0 commit comments

Comments
 (0)