This repository was archived by the owner on Mar 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
43 lines (33 loc) · 1.27 KB
/
Hangman.py
File metadata and controls
43 lines (33 loc) · 1.27 KB
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
import random
with open("words.txt", "r") as f:
text = f.read()
words = text.split()
word_count = len(words)
word = words[random.randint(0, word_count - 1)]
# word = 'onomatopoeia'
num_guesses = 10
hidden_word = '_' * len(word)
guess = 1
garbage_letters = []
while guess <= num_guesses and '_' in hidden_word:
if guess > 1:
print(f'Ruled-out letters: {garbage_letters}')
print(hidden_word)
user_input = input(f'Enter a character (guess #{guess}): ')
if len(user_input) == 1:
# Count the number of times the character occurs in the word
num_occurrences = word.count(user_input)
# Replace the appropriate position(s) in hidden_word with the actual character.
position = -1
for occurrence in range(num_occurrences):
position = word.find(user_input, position + 1) # Find the position of the next occurrence
hidden_word = hidden_word[:position] + user_input + hidden_word[
position + 1:] # Rebuild the hidden word string
if user_input not in word:
garbage_letters.append(user_input)
guess += 1
if '_' in hidden_word:
print('Loser!', end=' ')
else:
print('Winner!', end=' ')
print(f'The word was {word}.')