-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangManGame.py
164 lines (148 loc) · 4.22 KB
/
HangManGame.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
import random
def display_hangman(chances_left):
stages = [
"""
-----
| |
O |
/|\\ |
/ \\ |
|
=========
""",
"""
-----
| |
O |
/|\\ |
/ |
|
=========
""",
"""
-----
| |
O |
/|\\ |
|
|
=========
""",
"""
-----
| |
O |
/| |
|
|
=========
""",
"""
-----
| |
O |
| |
|
|
=========
""",
"""
-----
| |
O |
|
|
|
=========
""",
"""
-----
| |
|
|
|
|
=========
"""
]
return stages[chances_left]
# List of words for the Hangman game
words = [
"python", "javascript", "hangman", "algorithm", "function",
"variable", "syntax", "compiler", "interpreter", "debugging",
"loop", "conditional", "exception", "inheritance", "encapsulation","rishitha"
]
# Choose a random word from the list
word = random.choice(words)
# Initialize the guessed word with underscores
guessed_word = ["_"] * len(word)
# Set the total number of chances
total_chances = 6
# Welcome message and initial game state
print("Welcome to Hangman Game")
print(f"You have {total_chances} chances to guess the word")
print(display_hangman(total_chances))
print(" ".join(guessed_word))
# Game loop
while total_chances > 0 and "".join(guessed_word)!= word:
letter = input("Guess a letter: ").lower()
if letter in word:
# Update the guessed word with the correct letter
for index in range(len(word)):
if word[index] == letter:
guessed_word[index] = letter
print("Good guess!")
else:
# Decrement the number of chances if the guess is incorrect
total_chances -= 1
print("Incorrect guess")
# Display the current state of the guessed word and remaining chances
print(display_hangman(total_chances))
print(" ".join(guessed_word))
print(f"The remaining chances are: {total_chances}")
# Final game outcome
if "".join(guessed_word) == word:
print("Congratulations! You have guessed the word correctly")
else:
print("Game over")
print("You lose. All the chances are exhausted")
print(f"The word was: {word}")
# Additional features
print("\nWould you like to play again? (yes/no)")
response = input().lower()
if response == "yes":
print("Let's play again!")
# Reset the game state
word = random.choice(words)
guessed_word = ["_"] * len(word)
total_chances = 6
print("Welcome to Hangman Game")
print(f"You have {total_chances} chances to guess the word")
print(display_hangman(total_chances))
print(" ".join(guessed_word))
# Restart the game loop
while total_chances > 0 and "".join(guessed_word)!= word:
letter = input("Guess a letter: ").lower()
if letter in word:
# Update the guessed word with the correct letter
for index in range(len(word)):
if word[index] == letter:
guessed_word[index] = letter
print("Good guess!")
else:
# Decrement the number of chances if the guess is incorrect
total_chances -= 1
print("Incorrect guess!!!")
# Display the current state of the guessed word and remaining chances
print(display_hangman(total_chances))
print(" ".join(guessed_word))
print(f"The remaining chances are: {total_chances}")
# Final game outcome
if "".join(guessed_word) == word:
print("Congratulations! You have guessed the word correctly and saved the man's life!")
else:
print("Game over")
print("You lose. All the chances are exhausted and couldn't able to save his life ")
print(f"The word was: {word}")
else:
print("Thanks for playing!")