Skip to content

Commit 5e3d4f5

Browse files
authored
Add files via upload
1 parent e5b423a commit 5e3d4f5

File tree

21 files changed

+2221
-0
lines changed

21 files changed

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

0 commit comments

Comments
 (0)