-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangMan.py
More file actions
95 lines (76 loc) · 3.36 KB
/
Copy pathHangMan.py
File metadata and controls
95 lines (76 loc) · 3.36 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 OR GUESS THE WORD
import random
hangman_art = {0:(" ",
" ",
" "),
1: (" o ",
" ",
" "),
2: (" o ",
" | ",
" "),
3: (" o ",
"/| ",
" "),
4: (" o ",
"/|\\",
" "),
5: (" o ",
"/|\\",
"/ "),
6: (" o ",
"/|\\",
"/ \\")}
words = ("alligator", "ant","ape","badger", "bat", "bear", "bee", "bison", "boar", "buffalo", "butterfly", "camel","cat", "caterpillar", "cattle", "cheetah", "chicken", "chimpanzee", "clam", "cobra", "cockroach","crab", "crane","crocodile", "crow",
"deer", "dinosaur", "dog", "dolphin", "donkey","eagle", "elephant","falcon", "fish", "flamingo", "fly", "fox", "frog", "giraffe","goat", "goldfish", "goose", "gorilla","grasshopper",
"hamster", "hare", "hawk","hedgehog","horse", "human", "hummingbird", "hyena","jackal", "jaguar","jellyfish", "kangaroo", "kingfisher", "leopard", "lion","lobster",
"mongoose", "monkey", "moose", "mosquito", "mouse", "octopus","ostrich","owl", "ox", "oyster", "panda", "panther", "parrot", "penguin", "pig", "pigeon", "polar-bear", "porpoise",
"rabbit", "raccoon", "rat","red-deer", "red-panda", "reindeer", "rhinoceros","scorpion", "seahorse", "seal", "shark", "sheep", "snail", "snake", "sparrow", "spider","squid", "squirrel","swan",
"tiger","turkey", "turtle", "viper", "vulture", "walrus", "wasp" ,"whale", "wildcat", "wolf", "wolverine","worm", "yak", "zebra")
def dispaly_man(wrong_guesses):
print("*********************")
for line in hangman_art[wrong_guesses]:
print(line)
print("*********************")
def display_hint(hint):
print(" ".join(hint))
def display_answer(answer):
print(" ".join(answer))
def main():
answer = random.choice(words)
hint = ["_"]*len(answer)
wrong_guesses = 0
guessed_letter = set()
running = True
while running:
dispaly_man(wrong_guesses)
display_hint(hint)
guess = input("Enter a Letter: ").lower()
# EDGE CASES FOR THE INPUT
if len(guess) != 1 or not guess.isalpha():
print("INvalid")
continue
if guess in guessed_letter:
print(f"{guess} is already guessed")
continue
guessed_letter.add(guess) # TO THE GUESSED VALUES
# TO FLIP THE LETTER WITH THE EMPTY GAP
if guess in answer :
for idx in range(len(answer)) :
if answer[idx] == guess:
hint[idx] = guess
else :
wrong_guesses += 1
# TO CHECK WHETHER THE PLAYER WON OR LOSE
if "_" not in hint :
dispaly_man(wrong_guesses)
display_answer(answer)
print("YOU WON!!!")
running = False
elif wrong_guesses >= len(hangman_art)-1:
dispaly_man(wrong_guesses)
display_answer(answer)
print("YOU LOSE!!")
running = False
if __name__ == "__main__":
main()