-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
136 lines (108 loc) · 3.83 KB
/
hangman.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
with open('words.txt','r') as file:
word_list = [line.strip() for line in file]
#Choose a random word and assign it to the variable chosen_word.then print it
print('''
██╗░░██╗░█████╗░███╗░░██╗░██████╗░███╗░░░███╗░█████╗░███╗░░██╗
██║░░██║██╔══██╗████╗░██║██╔════╝░████╗░████║██╔══██╗████╗░██║
███████║███████║██╔██╗██║██║░░██╗░██╔████╔██║███████║██╔██╗██║
██╔══██║██╔══██║██║╚████║██║░░╚██╗██║╚██╔╝██║██╔══██║██║╚████║
██║░░██║██║░░██║██║░╚███║╚██████╔╝██║░╚═╝░██║██║░░██║██║░╚███║
╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝░╚═════╝░╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝
''')
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
|
|
|
|
=========''']
import random
#create a variable called 'lives' to keep track of the lives left.
#set lives equal to 6
lives = 6
chosen_word =random.choice(word_list)
#Create a variable "placeholder" with the same number of "-" as the number of letters in the word.
placeholder = ""
for position in range(len(chosen_word)-1):
placeholder += "-"
print(f"Word to guess: {placeholder}")
#Ask the user for a letter and store it in a variable guess. make guess lowercase
#Use a while loop to ask the user for guess again
game_over = False
correct_letters = []
while not game_over:
print(f"******************{lives}/6 LIVES LEFT****************")
guess = input("Guess a letter: ").lower()
print(guess)
#if the user has entered the letter which they have already guessed. let them know and print the letter
print(f"You've already guessed {guess}")
display = ""
#Check if guess is any of the letter in the chosen_word. print "Right" if it is, "Wrong" if it's not.
#chanhge the for loop so that we can keep the guess in display
for letter in chosen_word:
if letter == guess:
display += letter
correct_letters.append(guess)
elif letter in correct_letters:
display += letter
else:
display += "-"
print(display)
#if guess is not one of the letter in the chosen_word, Then reduce the lives by one.
#if lives goes down to 0 then the game should stop and it should print "You lose."
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}, that's not in the word. You lose a life.")
if lives == 0:
print("********************YOU LOSE**********************")
print(f"The word was {chosen_word}")
game_over = True
if "-" not in display:
game_over = True
print("**************YOU WIN*****************")
#Print the ASCII art from 'stages'
#that corresponds to current number of lives user has remaining
print(stages[lives])