Skip to content

Commit d2ce916

Browse files
authored
Puzzle.py
1 parent c0b68a7 commit d2ce916

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Door Puzzle AI/Puzzle.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import random
2+
3+
def generate_doors(num_doors):
4+
doors = [False] * num_doors
5+
prize_door = random.randint(0, num_doors - 1)
6+
doors[prize_door] = True
7+
return doors
8+
9+
def choose_door(num_doors):
10+
return random.randint(0, num_doors - 1)
11+
12+
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]])
14+
print(f"Door {reveal_door + 1} has no prize.")
15+
16+
def switch_door(chosen_door, num_doors):
17+
return random.choice([i for i in range(num_doors) if i != chosen_door])
18+
19+
def main():
20+
num_doors = 3
21+
doors = generate_doors(num_doors)
22+
23+
print("Welcome to the Door Puzzle AI!")
24+
print("There are three doors. Behind one of the doors is a prize.")
25+
print("You need to choose a door, and then we will reveal one door that does not have the prize.")
26+
print("You will then have the option to switch your chosen door or stick with the current choice.")
27+
print("Let's get started!")
28+
29+
chosen_door = choose_door(num_doors)
30+
print(f"\nYou have chosen Door {chosen_door + 1}.")
31+
32+
open_door(doors, chosen_door)
33+
34+
switch_choice = input("Do you want to switch your chosen door? (yes/no): ").lower()
35+
36+
if switch_choice == "yes":
37+
chosen_door = switch_door(chosen_door, num_doors)
38+
39+
if doors[chosen_door]:
40+
print("Congratulations! You have won the prize!")
41+
else:
42+
print("Sorry, you did not win this time. Better luck next time!")
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)