-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
107 lines (102 loc) · 3.15 KB
/
hangman.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
import random
def get_random_word_from_wordlist():
wordlist = []
with open("hangman_wordlist.txt", 'r') as file:
wordlist = file.read().split('\n')
word = random.choice(wordlist)
return word
def get_some_letters(word):
letters = []
temp = '_'*len(word)
for char in list(word):
if char not in letters:
letters.append(char)
character = random.choice(letters)
for num, char in enumerate(list(word)):
if char == character:
templist = list(temp)
templist[num] = char
temp = ''.join(templist)
return temp
def draw_hangman(chances):
if chances == 0:
print("----------")
print(" ( )-| ")
print(" - | - ")
print(" / \ ")
elif chances == 1:
print("----------")
print(" ( )- ")
print(" - | - ")
print(" / \ ")
elif chances == 2:
print("----------")
print(" ( ) ")
print(" - | - ")
print(" / \ ")
elif chances == 3:
print("----------")
print(" ( ) ")
print(" - | - ")
print(" / ")
elif chances == 4:
print("----------")
print(" ( ) ")
print(" - | - ")
print(" ")
elif chances == 5:
print("----------")
print(" ( ) ")
print(" | ")
print(" ")
elif chances == 6:
print("----------")
print(" ( ) ")
print(" ")
print(" ")
def start_hangman_game():
word = get_random_word_from_wordlist()
temp = get_some_letters(word)
chances = 7
found = False
while 1:
if chances == 0:
print(f"Sorry !!! You Lost, the word was: {word}")
break
print("=== Guess the word ===")
print(temp, end='')
print(f"\t(word has {len(word)} letters)")
print(f"Chances left: {chances}")
character = input("Enter the character you think the word may have: ")
if len(character) > 1 or not character.isalpha():
print("Please enter a single alphabet only")
continue
else:
for num, char in enumerate(list(word)):
if char == character:
templist = list(temp)
templist[num] = char
temp = ''.join(templist)
found = True
if found:
found = False
else:
chances -= 1
if '_' not in temp:
print(f"\nYou Won !!! The word was: {word}")
print(f"You got it in {7 - chances} chances")
break
else:
draw_hangman(chances)
print()
print("===== Welcome to Hangman Game =====")
while 1:
choice = input("Do you wanna play hangman (y/n): ")
if 'y' in choice.lower():
start_hangman_game()
elif 'n' in choice.lower():
print('Exiting...')
break
else:
print("Invalid input...please try again")
print("\n")