Python Day 7 Challange: Hangman Game
Published: 2023-08-10 09:58:33

Hangman Game Combines word knowledge, deduction and strategy.
In this game, one player think of a word and other player has to guess the word. In this game, only  predetermined number of incorrect guesses are allowed. For every incorrect guess, you end up taking a life a away from the man. He has only limited number of lives. After all incorrect guesses, man is hanged from gallow and player lose.
Here is the description of hangman program:
1. Word Selection : Word is chosen from predetermined list and kept secret from player.
2. Word Display : Each letter of Word is displayed using underscore, indicating length of the chosen word
3. Guess Game: The player guess a letter they think might be in the word. If the guess in the word, all the occurance of that letter is displayed in the correct position. if the guess is incorrect, one life is deducted from gallow figure and draw the remaining part of it and display. 
4. Win or Lose.   Guessing continous until player guess all letter correctly or make too many incorrect guesses. If player guess the word in first attempt or make all correct guess, he wins and saves the gallow man. Otherwise gallow man is hanged and player lose.

```
# Hangman Game in Python 3 --- Komal Gilani 

import random
import hangman_words
import hangman_art


def playagain():
    print('Do you want to play again? (yes or no)')
    return input().lower().startswith('y')


# TODO-1: - Update the word list to use the 'word_list' from hangman_words.py
# Delete this line: word_list = ["ardvark", "baboon", "camel"]



# TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.


# Create blanks



def start_game(chosen_word: object, lives: object,end_of_game) -> object:
    word_length = len(chosen_word)
    while not end_of_game:
        guess = input("Guess a letter: ").lower()

        # TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
        if guess in display:
            print(f"You've already guessed {guess}")
        if guess == chosen_word:
            end_of_game = True
            print("You win")
            return  end_of_game
        # Check guessed letter
        for position in range(word_length):
            letter = chosen_word[position]
            # print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")
            if letter == guess:
                display[position] = letter

        # Check if user is wrong.
        if guess not in chosen_word:

            # TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
            lives -= 1
            print(f"You guessed {guess}, that's not in the word, you lose a life".format(guess))
            if lives == 0:
                end_of_game = True
                print("You lose.")
                return end_of_game

        # Join all the elements in the list and turn it into a String.
        print(f"{' '.join(display)}")

        # Check if user has got all letters.
        if "_" not in display:
            end_of_game = True
            print("You win.")
            return end_of_game

        # TODO-2: - Import the stages from hangman_art.py and make this error go away.
        print(hangman_art.stages[lives])



# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    end_of_game = False
    chosen_word = random.choice(hangman_words.word_list)
    display = ['_' for x in range(len(chosen_word))]
    lives = 6
    print(hangman_art.logo)
    # Testing code
    print(f'Are you Ready??')
    end_of_game= start_game(chosen_word,lives,end_of_game)
    if end_of_game:
        if playagain():
            end_of_game = False
            chosen_word = random.choice(hangman_words.word_list)
            display = ['_' for x in range(len(chosen_word))]
            print(f'Start again -- The word is different??')
            end_of_game = start_game(chosen_word,lives,end_of_game)


```