PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#IMPORTS RANDOM
from random import randint
print("Ghost Game")
#SETS BRAVE TO TRUE
brave = True
#SETS SCORE TO THE STARTING VALUE OF 0
score = 0
#RUNS THE FOLLOWING CODE WHILE BRAVE IS TRUE
while brave:
#CHOOSES A RANDOM NUMBER FROM 1 TO 3, AND SETS THIS NUMBER TO THE DOOR WITH A GHOST BEHIND IT
ghost_door = randint(1, 3)
print("Three doors ahead...")
print("A ghost behind one.")
print("Which door will you open?")
#ASKS THE USER TO CHOOSE BETWEEN THE THREE DOORS
door = input("1, 2, or 3?")
door_num = int(door)
#IF THE USER ENTERED THE NUMBER WITH A GHOST BEHIND IT, THE PROGRAM EXECUTES THE FOLLOWING CODE
if door_num == ghost_door:
print("Ghost!")
#SETS BRAVE TO FALSE AND ENDS THE EXECUTION OF THIS LOOP
brave = False
#IF THE USER ENTERED A DOOR WITHOUT A GHOST, THE FOLLOWING CODE IS EXECUTED
else:
print("No ghost!")
print("You enter the next room.")
#ADDS 1 TO THE SCORE
score += 1
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run