generated from github/codespaces-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
153 lines (124 loc) · 3.96 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
144
145
146
147
148
149
150
151
152
153
import random
def play():
print_opening_message()
secret_word = load_secret_word()
guessed_letters = initialize_correct_letters(secret_word)
hanged = False
guessed = False
errors = 0
print(guessed_letters)
while not hanged and not guessed:
guess = ask_for_guess()
if guess in secret_word:
mark_correct_guess(guess, guessed_letters, secret_word)
else:
errors += 1
draw_hangman(errors)
hanged = errors == 7
guessed = "_" not in guessed_letters
print(guessed_letters)
if guessed:
print_winner_message()
else:
print_loser_message(secret_word)
print("End of the game")
def print_opening_message():
print("*********************************")
print("***Welcome to the Hangman Game!***")
print("*********************************")
def load_secret_word():
file = open("words.txt", "r")
words = []
for line in file:
line = line.strip()
words.append(line)
file.close()
number = random.randrange(0, len(words))
secret_word = words[number].upper()
return secret_word
def initialize_correct_letters(secret_word):
return ["_" for letter in secret_word]
def ask_for_guess():
guess = input("Which letter? ")
guess = guess.strip().upper()
return guess
def mark_correct_guess(guess, guessed_letters, secret_word):
index = 0
for letter in secret_word:
if (guess == letter):
guessed_letters[index] = letter
index += 1
def print_winner_message():
print("Congratulations, you won!")
print(" ___________ ")
print(" '._==_==_=_.' ")
print(" .-\\: /-. ")
print(" | (|:. |) | ")
print(" '-|:. |-' ")
print(" \\::. / ")
print(" '::. .' ")
print(" ) ( ")
print(" _.' '._ ")
print(" '-------' ")
def print_loser_message(secret_word):
print("Oops, you got hanged!")
print("The secret word was {}".format(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(errors):
print(" _______ ")
print(" |/ | ")
if(errors == 1):
print(" | (_) ")
print(" | ")
print(" | ")
print(" | ")
if(errors == 2):
print(" | (_) ")
print(" | \ ")
print(" | ")
print(" | ")
if(errors == 3):
print(" | (_) ")
print(" | \| ")
print(" | ")
print(" | ")
if(errors == 4):
print(" | (_) ")
print(" | \|/ ")
print(" | ")
print(" | ")
if(errors == 5):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | ")
if(errors == 6):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | / ")
if (errors == 7):
print(" | (_) ")
print(" | \|/ ")
print(" | | ")
print(" | / \ ")
print(" | ")
print("_|___ ")
print()
if __name__ == "__main__":
play()