Hangman is a classic game in which a player thinks of a word and the other player tries to guess that word within a certain amount of attempts.
This is an implementation of the Hangman game, where the computer thinks of a word and the user tries to guess it.
- The first step is setting up the GitHub repository for the Hangman Project.
- It involves creating the necessary files and documents that showcase professional documentation habits.
- Created a list containing the name of 5 fruits and assigned the list to the variable word_list.
- With the use of the module
random
, a random word is selected from the list. - The user is asked to input a single letter.
- Conditional checks are created that must be passed before the input is accepted.
To define the functions check_guess
and ask_for_input
for the Hangman game, I followed these steps:
- First, I defined the
check_guess
function which takes a guessed letter as a parameter and checks if the letter is in the secret word. I passed the guess as a parameter to the function and converted it to lowercase to ensure that the game is case-insensitive. - Next, I defined the
ask_for_input
function which is responsible for asking the user to guess a letter, checking if the input is valid, and checking if the guess is in the secret word. The function will continue to ask for input until a valid guess is made. - I also called the
check_guess
function from within theask_for_input
function to check if the guess is in the secret word. Thecheck_guess
function takes the guess as a parameter. - Finally, I called the
ask_for_input
function outside the function definition to test the code.
The Hangman class is defined in the milestone_4
file. It has the following attributes:
word_list
: A list of words from which the game selects a random word.num_lives
: The number of lives the player has to guess the word. Default value is 5.list_of_guesses
: A list that stores the letters guessed by the player.
The class also has the following methods:
__init__(self, word_list, num_lives=5)
: The initializer method that sets up the game. It takes theword_list
andnum_lives
as parameters, whereword_list
is a list of words andnum_lives
is an optional parameter with a default value of 5. It initializes the attributesword
,word_guessed
,num_letters
, and assigns a random word fromword_list
toword
.check_guess(self, guess)
: A method that checks if the guessed letter is in the word. It takes aguess
parameter and compares it to the letters in the word. If the guess is correct, it updates theword_guessed
list and decreases thenum_letters
count. If the guess is incorrect, it reduces thenum_lives
count.ask_for_input(self)
: A method that asks the player to input a letter guess. It validates the input and checks if the guess has already been made. If the input is valid and a new guess, it calls thecheck_guess
method and updates thelist_of_guesses
.
The milestone_5.py
script has been added to provide a complete game execution. It includes the following:
- The
play_game
function: This function takes aword_list
as a parameter. It sets the number of lives to 5, creates an instance of theHangman
class, and enters a while loop to continue the game until a game-over or victory condition is met. It calls theask_for_input
method to get the player's guesses and checks for the necessary conditions to break out of the loop. At the end, it prints appropriate messages indicating whether the player won or lost the game.
To play the Hangman Game, run the milestone_5.py
script and pass a list of words as an argument to the play_game
function. The game will interactively prompt you for letter guesses and provide feedback on the correctness of your guesses.
Example usage:
word_list = ["apple", "pear", "pineapple", "guava", "banana"]
play_game(word_list)