-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam.py
49 lines (42 loc) · 1.24 KB
/
exam.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
import random
def word_list():
with open('5_letter_words.txt', 'r') as file:
words = file.readlines()
return [word.strip().lower() for word in words]
def random_word(words):
return random.choice(words)
def is_real_word(guess, word_list):
return guess in word_list
def check_guess(guess, target_word):
result = ''
guessed_chars = set()
for i, char in enumerate(guess):
if char == target_word[i]:
result += 'X'
guessed_chars.add(char)
elif char in target_word and char not in guessed_chars:
result += 'O'
guessed_chars.add(char)
else:
result += '_'
return result
def next_guess(words):
while True:
guess = input("Please enter a guess: ").lower()
if is_real_word(guess, words):
return guess
else:
print("That's not a real word!")
def play():
words = word_list()
target_word = random_word(words)
for _ in range(6):
guess = next_guess(words)
result = check_guess(guess, target_word)
print(result)
if result == 'XXXXX':
print("You won!")
return
print("You lost!")
print(f"The word was: {target_word}")
play()