-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
128 lines (111 loc) · 3.28 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
import random
from animals import animals
import string
import colorama
from colorama import Fore
colorama.init()
def get_animal(animals):
animal = random.choice(animals)
# while ' ' in animal:
# animal = random.choice(animals)
return animal.upper()
def hangman():
animal = get_animal(animals)
word_letters = set(animal)
alphabet = set(string.ascii_uppercase)
used_letters = set()
lives = 6
print(Fore.MAGENTA + "\n-----Let's play Hangman!-----")
# getting user input
while len(word_letters) > 0 and lives > 0:
# letters used
print(Fore.BLUE + '\nYou have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))
# what the current word is (eg. C - T)
word_list = [letter if letter in used_letters else '-' for letter in animal]
print(Fore.WHITE + display_hangman(lives))
print(Fore.YELLOW + '\nCurrent word: ', ' '.join(word_list))
user_letter = input(Fore.CYAN + '\nEnter a letter:').upper()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
else:
lives = lives - 1
print('\nYour letter,', user_letter, 'is not in the word.')
elif user_letter in used_letters:
print(Fore.RED + '\n-----You have already used that letter, guess another letter.-----')
else:
print(Fore.RED + '\n-----Invalid character, please try again.-----')
if lives == 0:
print(Fore.RED + '\nYou died, sorry. The animal was', animal)
else:
print(Fore.GREEN + '\nCongratulations! You have guessed the animal', animal, '!!')
def display_hangman(lives):
stages = [
"""
---------
| |
| O
| \|/
| |
| / \
---
""",
"""
---------
| |
| O
| \|/
| |
| /
---
""",
"""
---------
| |
| O
| \|/
| |
|
---
""",
"""
---------
| |
| O
| \|/
|
|
---
""",
"""
---------
| |
| O
| |/
|
|
---
""",
"""
---------
| |
| O
|
|
|
---
""",
"""
---------
| |
|
|
|
|
---
"""
]
return stages[lives]
if __name__ == '__main__':
hangman()