-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangManGame.py
More file actions
95 lines (79 loc) · 2.45 KB
/
HangManGame.py
File metadata and controls
95 lines (79 loc) · 2.45 KB
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
# ----HangMan Game-------
import random
words = ["sankar", "sai", "satya", "raja", "ram"] # you can take random strings of your own
hangman_art = {0: (" ",
" ",
" "),
1: (" o ",
" ",
" "),
2: (" o ",
" | ",
" "),
3: (" o ",
"/| ",
" "),
4: (" o ",
"/|\\",
" "),
5: (" o ",
"/|\\",
"/ "),
6: (" o ",
"/|\\",
"/ \\")}
def art(wrong_guesses):
print("*************")
for art_man in hangman_art[wrong_guesses]:
print(art_man)
print("*************")
def hints(hint):
print(" ".join(hint))
def ans(answer):
print("--------------")
print(" ".join(answer))
print("--------------")
def main():
while True:
answer = random.choice(words)
# print(answer)
hint = ["_"] * len(answer)
# print(hint)
wrong_guesses = 0
guessed_letters = set()
while True:
art(wrong_guesses)
hints(hint)
# ans(answer)
guess = input('Enter the guessed letter: ').lower()
if len(guess) > 1 or not guess.isalpha():
print("Invalid guess enter single letter!")
continue
if guess in guessed_letters:
print("Already guessed!")
# wrong_guesses += 1
continue
guessed_letters.add(guess)
if guess in answer:
for i in range(len(answer)):
if answer[i] == guess:
hint[i] = guess
else:
wrong_guesses += 1
if "_" not in hint:
art(wrong_guesses)
ans(answer)
print("you Won the game🥳")
break
elif wrong_guesses >= len(hangman_art) - 1:
art(wrong_guesses)
print("you lose the game😓")
ans(answer)
print("👆 is the Right answer")
break
play_again = input("enter 'yes' to play again? ").lower()
if play_again != 'yes':
print("Thanks for playing! Goodbye!")
break
if __name__ == "__main__":
main()