forked from rebfrank/c2c-hangman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
77 lines (68 loc) · 1.89 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
hangman_parts = [ "head", "left arm", "torso", "right arm", "left leg", "right leg" ]
num_wrong_guesses_allowed = len(hangman_parts)
words = [
"apple",
"butterfly",
"car",
"pajama",
"kayak",
"zigzag",
"zombie",
"oxygen",
"able",
"baby",
"lock",
"ornament",
"quality",
"liquid",
"suggestion",
"weather",
"twist"
]
def draw_hangman(num_wrong_guesses):
if num_wrong_guesses > num_wrong_guesses_allowed:
num_wrong_guesses = num_wrong_guesses_allowed
hangman_characters = {
"head" : " O",
"left arm" : " /",
"torso" : "|",
"right arm" : "\\",
"left leg" : " /",
"right leg" : " \\"
}
hangman_newlines = [ "head", "right arm", "right leg" ]
output = " _____\n | |\n | "
num_newlines = 0
for i in range(num_wrong_guesses):
output = output + hangman_characters[hangman_parts[i]]
if hangman_parts[i] in hangman_newlines:
output = output + "\n | "
num_newlines = num_newlines + 1
for i in range(len(hangman_newlines) - num_newlines):
output = output + "\n |"
output = output + "____\n\n"
print(output)
def check_if_done(guesses, word):
for letter in word:
if letter not in guesses:
return False
return True
word = "test"
wrong_guesses = []
all_guesses = []
done = False
while not done and len(wrong_guesses) < num_wrong_guesses_allowed:
draw_hangman(wrong_guesses)
guess = input("Guess a letter: ")
all_guesses.append(guess)
if guess in word:
print("That's right!")
done = check_if_done(all_guesses, word)
else:
print("Nope, sorry!")
wrong_guesses.append(guess)
if wrong_guesses == num_wrong_guesses_allowed:
draw_hangman(wrong_guesses)
print("You've been hanged! The word was", word)
else:
print("Nice going! You got it!")