1
1
import random
2
2
from json import load
3
3
4
- #function to randomly get one word from words.py and convert the word to uppercase
4
+
5
+ # function to randomly get one word from words.py and convert the word to uppercase
5
6
def get_word ():
6
7
with open ('words.json' ) as json_file :
7
8
data = load (json_file )
8
-
9
9
wordArray = data ["word_list" ]
10
10
word = random .choice (wordArray )
11
11
word = word .upper ()
12
12
return word
13
13
14
- #function to play the game
14
+
15
+ # function to play the game
15
16
def play (word ):
16
17
17
- #intialise variable
18
- word_completion = "_" * len (word ) #generate a line to show the number of word
19
- guessed = False #indicate the status of guess
20
- guessed_letters = [] #store guessed letters
21
- guessed_words = [] #store guessed words
22
- tries = 6 #user have 6 times of wrong
23
-
24
- #display message and the format of the hangman
18
+ # intialise variable
19
+ word_completion = "_" * len (word ) # generate a line to show the number of word
20
+ guessed = False # indicate the status of guess
21
+ guessed_letters = [] # store guessed letters
22
+ guessed_words = [] # store guessed words
23
+ tries = 6 # user have 6 times of wrong
24
+ # display message and the format of the hangman
25
25
print ("Let's play Hangman!" )
26
26
print (display_hangman (tries ))
27
27
print (word_completion )
28
28
print ("\n " )
29
29
print ("Length of the word: " , len (word ))
30
30
print ("\n " )
31
31
32
- #user can keep guessing when the tries is more than 0 and the answer is not found yet.
32
+ # user can keep guessing when the tries is more than 0 and the answer is not found yet.
33
33
while not guessed and tries > 0 :
34
34
35
- #Display message and ask for user input and convert it into uppercase
35
+ # Display message and ask for user input and convert it into uppercase
36
36
guess = input ("Please guess a letter or the word: " ).upper ()
37
37
38
- #check the length of the user input and is it alpha or not
38
+ # check the length of the user input and is it alpha or not
39
39
if len (guess ) == 1 and guess .isalpha ():
40
40
41
- #display message when user guess the same letter twice
41
+ # display message when user guess the same letter twice
42
42
if guess in guessed_letters :
43
43
print ("You already guessed the letter" , guess )
44
44
45
- #display message and deduct the tries when user guess the wrong letter
45
+ # display message and deduct the tries when user guess the wrong letter
46
46
elif guess not in word :
47
47
print (guess , "is not in the word." )
48
48
tries -= 1
49
49
guessed_letters .append (guess )
50
50
51
- #dispay message and store the letter when the user guess the correct letter
51
+ # dispay message and store the letter when the user guess the correct letter
52
52
else :
53
53
print ("Good job," , guess , "is in the word!" )
54
54
guessed_letters .append (guess )
@@ -58,124 +58,126 @@ def play(word):
58
58
for index in indices :
59
59
word_as_list [index ] = guess
60
60
61
- #join the guess word in the word_completion
61
+ # join the guess word in the word_completion
62
62
word_completion = "" .join (word_as_list )
63
63
64
- #if there is not blank space in word_completion change the status of guess to true
64
+ # if there is not blank space in word_completion change the status of guess to true
65
65
if "_" not in word_completion :
66
66
guessed = True
67
67
68
- #check the length of the user input and is it alpha or not
68
+ # check the length of the user input and is it alpha or not
69
69
elif len (guess ) == len (word ) and guess .isalpha ():
70
- #display message when user guess the same letter twice
70
+ # display message when user guess the same letter twice
71
71
if guess in guessed_words :
72
72
print ("You already guessed the word" , guess )
73
73
74
- #display message and deduct the tries when user guess the wrong letter
74
+ # display message and deduct the tries when user guess the wrong letter
75
75
elif guess != word :
76
76
print (guess , "is not the word." )
77
- tries -= 1
77
+ tries -= 1
78
78
guessed_words .append (guess )
79
79
80
- #change the status of guess
81
- else :
80
+ # change the status of guess
81
+ else :
82
82
guessed = True
83
83
word_completion = word
84
84
85
- #display error message for user
85
+ # display error message for user
86
86
else :
87
87
print ("Not a valid guess." )
88
88
89
- #display the format of hangman each time of guess
89
+ # display the format of hangman each time of guess
90
90
print (display_hangman (tries ))
91
91
print (word_completion )
92
92
print ("\n " )
93
93
print ("Length of the word: " , len (word ))
94
94
print ("\n " )
95
95
96
- #if the variable of guess is true means user win the game
96
+ # if the variable of guess is true means user win the game
97
97
if guessed :
98
98
print ("Congrats, you guessed the word! You win!" )
99
-
100
- #else means user lose the game.
99
+ # else means user lose the game.
101
100
else :
102
101
print ("Sorry, you ran out of tries. The word was " + word + ". Maybe next time!" )
103
102
104
- #function to display the format of hangman
103
+
104
+ # function to display the format of hangman
105
105
def display_hangman (tries ):
106
- stages = [ """
106
+ stages = ["""
107
107
--------
108
108
| |
109
109
| 0
110
110
| \\ |/
111
111
| |
112
112
| / \\
113
113
-
114
- """ ,
115
- """
114
+ """ ,
115
+ """
116
116
--------
117
117
| |
118
118
| 0
119
119
| \\ |/
120
120
| |
121
121
| /
122
122
-
123
- """ ,
124
- """
123
+ """ ,
124
+ """
125
125
--------
126
126
| |
127
127
| 0
128
128
| \\ |/
129
129
| |
130
130
|
131
131
-
132
- """ ,
133
- """
132
+ """ ,
133
+ """
134
134
--------
135
135
| |
136
136
| 0
137
137
| \\ |
138
138
| |
139
139
|
140
140
-
141
- """ ,
142
- """
141
+ """ ,
142
+ """
143
143
--------
144
144
| |
145
145
| 0
146
146
| |
147
147
| |
148
148
|
149
149
-
150
- """ ,
151
- """
150
+ """ ,
151
+ """
152
152
--------
153
153
| |
154
154
| 0
155
155
|
156
156
|
157
157
|
158
158
-
159
- """ ,
160
- """
159
+ """ ,
160
+ """
161
161
--------
162
162
| |
163
163
|
164
164
|
165
165
|
166
166
|
167
167
-
168
- """
169
- ]
168
+ """
169
+ ]
170
170
return stages [tries ]
171
171
172
- #main function to start the game
172
+
173
+ # main function to start the game
173
174
def main ():
174
175
word = get_word ()
175
176
play (word )
176
177
while input ("Play Again? (Y/N): " ).upper () == "Y" :
177
178
word = get_word ()
178
179
play (word )
179
180
181
+
180
182
if __name__ == "__main__" :
181
183
main ()
0 commit comments