-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
143 lines (124 loc) · 4.16 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
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
import random
def play():
print_welcome()
secret_word = load_word()
word_letters = ["_" for l in secret_word] #List comprehension -> loop to increase '_' for each letter (l) in secret_word
guessed = False
died = False
misses: int = 0
while (not died and not guessed ):
guess = input("Try to guess a letter:\n").strip().upper()
if (guess in secret_word):
input_discovered_letters(secret_word,guess,word_letters)
else:
misses += 1
draw_hangman(misses)
died = misses == 7
guessed = '_' not in word_letters
print(word_letters)
if (guessed):
win_message()
elif (died):
loser_message(secret_word)
print("\nThe end.")
#####Game Functions#####
#Docstrings immediately after functions headers
def print_welcome():
"""Function that calls welcome message"""
print(26 * "~")
print("Welcome to the Hangman game!")
print(26 * "~")
def load_word():
"""Function that returns random word from .txt"""
file = open("words.txt", "r")
words = []
for line in file:
line = line.strip()
words.append(line)
file.close()
num = random.randrange(0, len(words))
secret_word = words[num].upper()
return secret_word
def input_discovered_letters(secret_word,guess,word_letters):
"""Function that substitue "_" by a guessed word,"""
n = 0
for letter in secret_word: #loop that recognize right words in secret_word, then input these letters in word_letters[] to show the user guessed words
if (letter == guess):
word_letters[n] = letter
n += 1
def win_message():
"""Function that calls winners message"""
print("Congratulations, you save him from the hangman!!")
print(" ___________ ")
print(" '._==_==_=_.' ")
print(" .-\\: /-. ")
print(" | (|:. |) | ")
print(" '-|:. |-' ")
print(" \\::. / ")
print(" '::. .' ")
print(" ) ( ")
print(" _.' '._ ")
print(" '-------' ")
def loser_message(secret_word):
"""Function that calls losers message"""
print("Oh dear, your effort wasn't enough to save him!!")
print(f"The Answer was {secret_word}")
print(" _______________ ")
print(" / \ ")
print(" / \ ")
print("// \/\ ")
print("\| XXXX XXXX | / ")
print(" | XXXX XXXX |/ ")
print(" | XXX XXX | ")
print(" | | ")
print(" \__ XXX __/ ")
print(" |\ XXX /| ")
print(" | | | | ")
print(" | I I I I I I I | ")
print(" | I I I I I I | ")
print(" \_ _/ ")
print(" \_ _/ ")
print(" \_______/ ")
def draw_hangman(misses):
print(" _______ ")
print(" |/ | ")
if(misses == 1):
print(" | (_) ")
print(" | ")
print(" | ")
print(" | ")
if(misses == 2):
print(" | (_) ")
print(" | \ ")
print(" | ")
print(" | ")
if(misses == 3):
print(" | (_) ")
print(" | \| ")
print(" | ")
print(" | ")
if(misses == 4):
print(" | (_) ")
print(" | \|/ ")
print(" | ")
print(" | ")
if(misses == 5):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | ")
if(misses == 6):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | / ")
if (misses== 7):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | / \ ")
print(" | ")
print("_|___ ")
print()
if (__name__ == "__main__"):
play()