-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update README.md and create milestone_4.py. This contains the class H…
…angman
- Loading branch information
1 parent
d9a055c
commit d12bc78
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import random | ||
# class definition for hangman | ||
class Hangman: | ||
# hangman constructor | ||
def __init__ (self, word_list, num_lives=5): | ||
# attributes | ||
self.word = random.choice(word_list) | ||
self.word_guessed = ['_' for _ in self.word] | ||
self.num_letters = len(set(self.word)) | ||
self.word_list = word_list | ||
self.num_lives = num_lives | ||
self.list_of_guesses = [] | ||
|
||
# methods | ||
def check_guess(self, guess): | ||
guess = guess.lower() | ||
if guess in self.word: | ||
print(f'Good guess! {guess} is in the word.') | ||
for i in range(len(self.word)): | ||
if self.word[i] == guess: | ||
self.word_guessed[i] = guess | ||
self.num_letters -= 1 | ||
else: | ||
self.num_lives -= 1 | ||
print(f'Sorry, {guess} is not in the word') | ||
print(f'You have {self.num_lives} lives left') | ||
def ask_for_input(self): | ||
while True: | ||
guess = input('Guess a single letter: ') | ||
if not (len(guess) == 1 and guess.isalpha()): | ||
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) | ||
|
||
# test the code | ||
hangman = Hangman(['apple', 'banana', 'cherry']) | ||
hangman.ask_for_input() |