-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmilestone_5.py
More file actions
74 lines (62 loc) · 2.66 KB
/
milestone_5.py
File metadata and controls
74 lines (62 loc) · 2.66 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
class Hangman:
def __init__(self, word_list, num_lives=5):
# Initialize the Hangman game with a list of words and the number of lives
self.word_list = word_list
self.num_lives = num_lives
self.word = self._choose_random_word() # Select a random word from the list
self.word_guessed = ['_'] * len(self.word)
self.num_letters = len(set(self.word))
self.list_of_guesses = []
def _choose_random_word(self):
# Helper function to choose a random word from the word list
return random.choice(self.word_list)
def update_word_guessed(self, guess):
# Update the word_guessed list based on the correct guess
for i, letter in enumerate(self.word):
if letter == guess:
self.word_guessed[i] = guess
self.num_letters -= 1
def _handle_correct_guess(self, guess):
# Handle a correct guess, updating the guessed word
print(f"Good guess! '{guess}' is in the word.")
self.update_word_guessed(guess)
def _handle_incorrect_guess(self, guess):
# Handle an incorrect guess, decrement lives and provide feedback
self.num_lives -= 1
print(f"Sorry, '{guess}' is not in the word.")
print(f"You have {self.num_lives} lives left.")
def check_guess(self, guess):
# Check if the guess is correct or incorrect and take appropriate actions
guess = guess.lower()
if guess in self.word:
self._handle_correct_guess(guess)
else:
self._handle_incorrect_guess(guess)
def ask_for_input(self):
# Ask the player for a guess, handle invalid inputs and check the guess
while True:
guess = input("Guess a letter: ")
if not (guess.isalpha() and len(guess) == 1):
print("Invalid letter. Please, enter a single alphabetical character.")
elif guess in self.list_of_guesses:
print("You already tried that letter!")
else:
self.check_guess(guess)
self.list_of_guesses.append(guess)
break
def play_game(self):
# Main game loop that continues until the player wins or loses
while True:
if self.num_lives == 0:
print("You lost!")
break
elif self.num_letters > 0:
self.ask_for_input()
else:
print("Congratulations. You won the game!")
break
# You can add any words you like to the word_list
word_list = ['apple', 'banana', 'orange', 'tangerine', 'peach']
hangman_game = Hangman(word_list)
hangman_game.play_game()