-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathword_guessing_game.py
More file actions
74 lines (59 loc) · 1.63 KB
/
Copy pathword_guessing_game.py
File metadata and controls
74 lines (59 loc) · 1.63 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
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
import random
import re
def read_words():
try:
with open('words.txt', 'r') as file:
words = file.read().splitlines()
return words
except FileNotFoundError:
print('words.txt does not exist.')
return []
def display_word(secret_word, guessed_letters):
word_to_display = ''
for letter in secret_word:
if letter in guessed_letters:
word_to_display += letter
else:
word_to_display += '_'
print(word_to_display)
def get_guess(guessed_letters):
while True:
guess = input('Enter a letter: ').lower()
if len(guess) != 1:
print('Enter only one letter.')
elif not re.search('[a-z]', guess):
print('Enter only letters from a to z.')
elif guess in guessed_letters:
print('You already guessed that letter.')
else:
return guess
def is_word_guessed(secret_word, guessed_letters):
for letter in secret_word:
if letter not in guessed_letters:
return False
return True
def main():
words = read_words()
if not words:
print('No words loaded.')
return
secret_word = random.choice(words)
print(secret_word)
attempts = 6
guessed_letters = []
while attempts > 0:
display_word(secret_word, guessed_letters)
guess = get_guess(guessed_letters)
guessed_letters.append(guess)
if guess in secret_word:
print('Good guess')
if is_word_guessed(secret_word, guessed_letters):
print('Congratulations! You guessed the word')
break
else:
print('Wrong guess')
attempts -= 1
if attempts == 0:
print(f'Game over! The word was {secret_word}')
if __name__ == '__main__':
main()