-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
165 lines (154 loc) · 4.01 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
154
155
156
157
158
159
160
161
162
163
164
165
# Command-line Hangman
import random
WORD_LIST_CODING = ("Variable",
"Function",
"Loop",
"Algorithm",
"Array",
"Object",
"Class",
"Method",
"Parameter",
"Interface",
"Exception",
"Compiler",
"Framework",
"Debugging",
"Database",
"Pointer",
"Recursion",
"Syntax",
"Boolean",
"Library")
# Get a random word from word list
def get_word(word_list: list) -> str:
return random.choice(word_list)
# Print hangman based on the remaining tries
def display_hangman(tries: int) -> None:
stages = (
'''
--------
| / |
|/
|
|
|
-
''',
'''
--------
| / |
|/ O
|
|
|
-
''',
'''
--------
| / |
|/ O
| |
| |
|
-
''',
'''
--------
| / |
|/ O
| \\|
| |
|
-
''',
'''
--------
| / |
|/ O
| \\|/
| |
|
-
''',
'''
--------
| / |
|/ O
| \\|/
| |
| /
-
''',
'''
--------
| / |
|/ O
| \\|/
| |
| / \\
-
'''
)
print(stages[tries])
# Display the current state of the secret word with guessed letters revealed
def display_word_completion(secret_word: str, guessed_letters: set) -> None:
# Creating a string with guessed letters revealed and underscores for unrevealed letters
result = ''.join(char if char.lower() in guessed_letters else '_' for char in secret_word)
print(f"Word: {result}")
# Prompt the user to guess a letter or the whole word
def get_user_input() -> str:
guess = input("Guess a letter or the whole word: ").lower().strip()
return guess
# Play the Hangman game with the given secret word
def play(secret_word: str) -> str:
while True: # Main game loop
guessed = False
guessed_letters = set()
tries = 6
while tries > 0: # Current round loop
display_hangman(tries)
display_word_completion(secret_word, guessed_letters)
while True: # User guess loop
guess = get_user_input()
if not guess.isalpha():
print("Invalid input! Please enter only letters.")
else:
# Check if the input is a single letter or the whole word
if len(guess) > 1:
if guess == secret_word.lower():
guessed = True
break
else:
print("Invalid input! Please enter a letter or the whole word")
else:
guessed_letters.add(guess)
# Check if all letters in the secret word have been guessed
if ''.join(sorted(set(secret_word.lower()))) == ''.join(sorted(set(guessed_letters))):
guessed = True
break
if guessed:
guessed_words.append(secret_word)
break
else:
tries -= 1
continue
if guessed:
print(f'You have guessed the Secred word "{secret_word}" in {6-tries} guesses.')
return secret_word
else:
display_hangman(tries)
display_word_completion(secret_word, guessed_letters)
print("GAME OVER! The Hangman got You!")
print(f"The Secred word was: {secret_word}.")
break
def main():
print("\nWelcome to Hangman game!")
guessed_words = [] # Guessed words
while True:
play(get_word(WORD_LIST_CODING))
print(f"You guessed {len(guessed_words)} words.")
if input("Do you want to play again? (y/n): ").lower().strip() != "y":
break
if __name__ == '__main__':
main()