-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (53 loc) · 1.95 KB
/
main.py
File metadata and controls
70 lines (53 loc) · 1.95 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
# Please Subscribe my youtube channel "@problemsolvewithridoy"
"""
The WordGuess game where the player tries to guess a secret word by providing letter guesses.
"""
import random
LEXICON_FILE = "Lexicon.txt" # File to read word list from
INITIAL_GUESSES = 8 # Max number of guesses per game
def play_game(secret_word):
"""
Plays the WordGuess game with the provided secret word.
Args: secret_word (str): The word the player needs to guess.
"""
guessed_word = ['-'] * len(secret_word)
guesses_left = INITIAL_GUESSES
while True:
print('The word now looks like this:', ''.join(guessed_word))
print('You have', guesses_left, 'guesses left')
guess = input('Type a single letter here, then press enter: ').upper()
if len(guess) != 1:
print('Guess should only be a single character.')
continue
if guess in secret_word:
for i in range(len(secret_word)):
if secret_word[i] == guess:
guessed_word[i] = guess
else:
print('There are no', guess + "'s in the word")
guesses_left -= 1
if '-' not in guessed_word:
print('Congratulations, the word is:', secret_word)
break
if guesses_left == 0:
print('Sorry, you lost. The secret word was:', secret_word)
break
def get_word():
"""
Returns a secret word that the player is trying to guess in the game.
The word is randomly selected from a list of words read from the file specified by LEXICON_FILE.
Returns:
str: The secret word.
"""
with open(LEXICON_FILE, 'r') as file:
word_list = file.read().splitlines()
return random.choice(word_list)
def main():
"""
The entry point of the program.
Selects a secret word and starts the WordGuess game.
"""
secret_word = get_word()
play_game(secret_word)
if __name__ == "__main__":
main()