-
Notifications
You must be signed in to change notification settings - Fork 0
/
milestone_5.py
103 lines (92 loc) · 3.53 KB
/
milestone_5.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
from random import choice as random_choice
class Hangman:
'''
A Hangman Game that asks the user for a letter and checks if it is in the word.
It starts with a default number of lives and a random word from the word_list.
Parameters:
----------
word_list: list
List of words to be used in the game
num_lives: int
Number of lives the player has
Attributes:
----------
word: str
The word to be guessed picked randomly from the word_list
word_guessed: list
A list of the letters of the word, with '_' for each letter not yet guessed
For example, if the word is 'apple', the word_guessed list would be ['_', '_', '_', '_', '_']
If the player guesses 'a', the list would be ['a', '_', '_', '_', '_']
num_letters: int
The number of UNIQUE letters in the word that have not been guessed yet
num_lives: int
The number of lives the player has
list_of_guesses: list
A list of the letters that have already been tried
Methods:
-------
check_letter(letter)
Checks if the letter is in the word.
ask_letter()
Asks the user for a letter.
'''
def __init__(self, word_list, num_lives = 5):
self.word = random_choice(word_list)
self.word_guessed = ['_'] * len(self.word)
self.num_letters = len(set([chara for chara in self.word]))
self.num_lives = num_lives
self.word_list = word_list
self.list_of_guesses = []
def check_guess(self, guess):
'''
Checks if the letter is in the word.
If it is, it replaces the '_' in the word_guessed list with the letter.
If it is not, it reduces the number of lives by 1.
Parameters:
----------
guess: str
The letter to be checked
'''
if guess in self.word:
print(f"Good guess! {guess} is in the word.")
for index in range(len(self.word)):
if self.word[index] == guess:
self.word_guessed[index] = guess
self.num_letters -= 1
else:
self.num_lives -= 1
print(f"Sorry, {guess} is not in the word.\nYou have {self.num_lives} " + ("lives left." if self.num_lives > 1 else "life left."))
def ask_for_input(self):
'''
Asks the user for a letter and checks two things:
1. If the letter has already been tried
2. If the character is a single character
If it passes both checks, it calls the check_guess method.
'''
while True:
guess = input("Guess a letter: ")
guess = guess.lower()
if len(guess) != 1 or not guess.isalpha():
print("Invalid letter. Please, enter a single alphabetical character.")
elif guess in self.list_of_guesses:
print("You have already tried that letter!")
else:
self.check_guess(guess)
self.list_of_guesses.append(guess)
break
def play_game(word_list):
num_lives = 5
game = Hangman(word_list, num_lives)
while True:
if game.num_lives == 0:
print(f"You lost! The word was {game.word}.")
break
elif game.num_letters > 0:
print(''.join(map(str, game.word_guessed)))
game.ask_for_input()
else:
print("Congratulations. You won the game!")
break
if __name__ == "__main__":
with open("words.txt", "r") as words:
play_game(words.read().split())