11import random
22from json import load
33
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
56def get_word ():
67 with open ('words.json' ) as json_file :
78 data = load (json_file )
8-
99 wordArray = data ["word_list" ]
1010 word = random .choice (wordArray )
1111 word = word .upper ()
1212 return word
1313
14- #function to play the game
14+
15+ # function to play the game
1516def play (word ):
1617
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
2525 print ("Let's play Hangman!" )
2626 print (display_hangman (tries ))
2727 print (word_completion )
2828 print ("\n " )
2929 print ("Length of the word: " , len (word ))
3030 print ("\n " )
3131
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.
3333 while not guessed and tries > 0 :
3434
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
3636 guess = input ("Please guess a letter or the word: " ).upper ()
3737
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
3939 if len (guess ) == 1 and guess .isalpha ():
4040
41- #display message when user guess the same letter twice
41+ # display message when user guess the same letter twice
4242 if guess in guessed_letters :
4343 print ("You already guessed the letter" , guess )
4444
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
4646 elif guess not in word :
4747 print (guess , "is not in the word." )
4848 tries -= 1
4949 guessed_letters .append (guess )
5050
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
5252 else :
5353 print ("Good job," , guess , "is in the word!" )
5454 guessed_letters .append (guess )
@@ -58,124 +58,126 @@ def play(word):
5858 for index in indices :
5959 word_as_list [index ] = guess
6060
61- #join the guess word in the word_completion
61+ # join the guess word in the word_completion
6262 word_completion = "" .join (word_as_list )
6363
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
6565 if "_" not in word_completion :
6666 guessed = True
6767
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
6969 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
7171 if guess in guessed_words :
7272 print ("You already guessed the word" , guess )
7373
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
7575 elif guess != word :
7676 print (guess , "is not the word." )
77- tries -= 1
77+ tries -= 1
7878 guessed_words .append (guess )
7979
80- #change the status of guess
81- else :
80+ # change the status of guess
81+ else :
8282 guessed = True
8383 word_completion = word
8484
85- #display error message for user
85+ # display error message for user
8686 else :
8787 print ("Not a valid guess." )
8888
89- #display the format of hangman each time of guess
89+ # display the format of hangman each time of guess
9090 print (display_hangman (tries ))
9191 print (word_completion )
9292 print ("\n " )
9393 print ("Length of the word: " , len (word ))
9494 print ("\n " )
9595
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
9797 if guessed :
9898 print ("Congrats, you guessed the word! You win!" )
99-
100- #else means user lose the game.
99+ # else means user lose the game.
101100 else :
102101 print ("Sorry, you ran out of tries. The word was " + word + ". Maybe next time!" )
103102
104- #function to display the format of hangman
103+
104+ # function to display the format of hangman
105105def display_hangman (tries ):
106- stages = [ """
106+ stages = ["""
107107 --------
108108 | |
109109 | 0
110110 | \\ |/
111111 | |
112112 | / \\
113113 -
114- """ ,
115- """
114+ """ ,
115+ """
116116 --------
117117 | |
118118 | 0
119119 | \\ |/
120120 | |
121121 | /
122122 -
123- """ ,
124- """
123+ """ ,
124+ """
125125 --------
126126 | |
127127 | 0
128128 | \\ |/
129129 | |
130130 |
131131 -
132- """ ,
133- """
132+ """ ,
133+ """
134134 --------
135135 | |
136136 | 0
137137 | \\ |
138138 | |
139139 |
140140 -
141- """ ,
142- """
141+ """ ,
142+ """
143143 --------
144144 | |
145145 | 0
146146 | |
147147 | |
148148 |
149149 -
150- """ ,
151- """
150+ """ ,
151+ """
152152 --------
153153 | |
154154 | 0
155155 |
156156 |
157157 |
158158 -
159- """ ,
160- """
159+ """ ,
160+ """
161161 --------
162162 | |
163163 |
164164 |
165165 |
166166 |
167167 -
168- """
169- ]
168+ """
169+ ]
170170 return stages [tries ]
171171
172- #main function to start the game
172+
173+ # main function to start the game
173174def main ():
174175 word = get_word ()
175176 play (word )
176177 while input ("Play Again? (Y/N): " ).upper () == "Y" :
177178 word = get_word ()
178179 play (word )
179180
181+
180182if __name__ == "__main__" :
181183 main ()
0 commit comments