-
Notifications
You must be signed in to change notification settings - Fork 1
/
hangman_game.py
178 lines (139 loc) · 4.38 KB
/
hangman_game.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
print("""
Welcome to the Hangman game!
INSTRUCTIONS:
One player thinks of a word.
The other player (guessing player) tries to guess it by suggesting letters. If the player does not guess the word within 6 guesses, they lose.
The word to guess is represented by a row of dashes, representing each letter of the word.
If the guessing player suggests a letter which occurs in the word, all occurences of that letter appear in the correct positions and the number of guessess remains unchanged.
If the suggested letter does not occur in the word, one element of a hanged man stick figure is drawn and the number of guesses is increased by 1.
NOTE: You cannot guess the whole word in a single try. You have to type its letters one by one.
Game Begins! \n\n
""")
def hangman():
total_guesses = 6
global guesses
guesses = 0
global answer
answer = list(input("Enter a word for the other player to guess: "))
print("")
placeholder = list("_" * len(answer))
print("The word is:", " ".join(placeholder), end="\n\n")
global tried_letters
tried_letters = []
while guesses < total_guesses:
user_guess = input("Guess a letter: ")
if len(user_guess) != 1 or user_guess.isalpha() != True:
print("Enter a valid input. Please try again! \n")
continue
if user_guess in tried_letters:
print("You have already tried that letter. Please enter another letter. \n")
continue
if user_guess in answer:
print(f"Yes, '{user_guess}' is a part of the word!")
for ind, val in enumerate(answer):
if user_guess == val:
placeholder[ind] = val
print("Word:"," ".join(placeholder))
else:
print(f"'{user_guess}' is not a part of the word")
tried_letters.append(user_guess)
guesses += 1
print(f"You have {total_guesses - guesses} guesses left! Try guessing again.")
print("Incorrect Letters:", tried_letters)
stick_figure()
if placeholder == answer:
print("\nCongrats, you WIN! You have guessed the right word! \n\n")
play_again()
break
print("")
def stick_figure():
if guesses == 1:
print("""
_______
| |
O |
|
|
|
|
|
|
-------
""")
elif guesses == 2:
print("""
_______
| |
O |
! |
! |
|
|
|
|
-------
""")
elif guesses == 3:
print("""
_______
| |
O |
! |
! |
/ |
|
|
|
-------
""")
elif guesses == 4:
print("""
_______
| |
O |
! |
! |
/ \ |
|
|
|
-------
""")
elif guesses == 5:
print("""
_______
| |
O |
\! |
! |
/ \ |
|
|
|
-------
""")
elif guesses == 6:
print("""
_______
| |
O |
\!/ |
! |
/ \ |
|
|
|
-------
""")
print("You are out of guesses")
print("YOU LOSE!")
print("The correct word was:", "".join(answer), end = "\n\n\n")
play_again()
def play_again():
again = input("Do you want to try again? [Yes/No] \n")
if again == "Yes":
hangman()
else:
print("Thank you for playing!")
if __name__ == "__main__":
hangman()