1
1
import random
2
2
3
+
3
4
def generate_doors (num_doors ):
4
5
doors = [False ] * num_doors
5
6
prize_door = random .randint (0 , num_doors - 1 )
6
7
doors [prize_door ] = True
7
8
return doors
8
9
10
+
9
11
def choose_door (num_doors ):
10
12
return random .randint (0 , num_doors - 1 )
11
13
14
+
12
15
def open_door (doors , chosen_door ):
13
- reveal_door = random .choice ([i for i in range (len (doors )) if i != chosen_door and not doors [i ]])
16
+ reveal_door = random .choice (
17
+ [i for i in range (len (doors )) if i != chosen_door and not doors [i ]])
14
18
print (f"Door { reveal_door + 1 } has no prize." )
15
19
20
+
16
21
def switch_door (chosen_door , num_doors ):
17
22
return random .choice ([i for i in range (num_doors ) if i != chosen_door ])
18
23
24
+
19
25
def main ():
20
26
num_doors = 3
21
27
doors = generate_doors (num_doors )
@@ -31,15 +37,17 @@ def main():
31
37
32
38
open_door (doors , chosen_door )
33
39
34
- switch_choice = input ("Do you want to switch your chosen door? (yes/no): " ).lower ()
40
+ switch_choice = input (
41
+ "Do you want to switch your chosen door? (yes/no): " ).lower ()
35
42
36
43
if switch_choice == "yes" :
37
44
chosen_door = switch_door (chosen_door , num_doors )
38
-
45
+
39
46
if doors [chosen_door ]:
40
47
print ("Congratulations! You have won the prize!" )
41
48
else :
42
49
print ("Sorry, you did not win this time. Better luck next time!" )
43
50
51
+
44
52
if __name__ == "__main__" :
45
53
main ()
0 commit comments