Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

HANGMAN GAME

INTRODUCTION:
Welcome to Hangman, a classic word-guessing game where your goal
is to figure out the hidden word before it's too late! The rules are
simple:

1. A secret word is chosen, and its letters are represented by dashes.


2. You have a limited number of guesses to figure out the word.
3. Each time you guess a letter, if it's in the word, those letters are
revealed. If not, you lose one of your guesses.
4. Be careful! If you make too many incorrect guesses, you'll be
"hanged" and the game ends.

Are you ready to test your vocabulary skills and save the hangman
from his fate? Let's play!
HOW TO PLAY HANGMAN GAME ?
To play Hangman, follow these steps:
1. Setup: One player (the "host") thinks of a word and writes down a
dash for each letter of that word, representing the unknown letters.
For example, if the word is "apple", they would write "_ _ _ _ _".
2. Guessing: The other player (the "guesser") starts guessing letters
that they think might be in the word. They can guess one letter at a
time.
3. Revealing Letters: If the guessed letter is in the word, the host
reveals all occurrences of that letter in the word. For example, if the
guess is "p" and the word is "apple", the host would reveal the two
"p"s: "_ p p _ _".
4. Incorrect Guesses: If the guessed letter is not in the word, the host
starts drawing parts of the hangman as a penalty. Commonly, there are
6 parts to draw: head, body, left arm, right arm, left leg, and right leg.
5. Continuation: The guesser continues guessing letters until they
either correctly guess the word (filling in all the blanks) or make too
many incorrect guesses, resulting in the completion of the hangman
figure.
6. Outcome: The game ends when the guesser either successfully
guesses the word or the hangman is fully drawn. If the guesser guesses
the word correctly, they win. If the hangman is completed before the
word is guessed, the host wins.

Remember, the number of incorrect guesses allowed before the


hangman is completed varies, but it's typically around six. Let the
guessing begin!
CODE:
import random
def choose_word():
words = ["apple", "banana", "orange", "grape", "pineapple"]
return random.choice(words)

def display_word(word, guessed_letters):


display = ""
for letter in word:
if letter in guessed_letters:
display += letter

else:
display += "_"
return display

def hangman():
word = choose_word()
guessed_letters = []
attempts = 6

print("Welcome to Hangman!")
print("Try to guess the word.")
while attempts > 0:
print("\nAttempts left:", attempts)
print("Word:", display_word(word, guessed_letters))

guess = input("Enter a letter: ").lower()

if len(guess) != 1 or not guess.isalpha():


print("Please enter a single letter.")
continue

if guess in guessed_letters:
print("You've already guessed that letter.")
continue

guessed_letters.append(guess)

if guess not in word:


attempts -= 1
print("Incorrect guess!")
if attempts == 0:
print("Sorry, you lost. The word was:", word)
break
else:
print("Correct guess!")

if all(letter in guessed_letters for letter in word):


print("Congratulations, you guessed the word:", word)
break

play_again = input("Do you want to play again? (yes/no): ").lower()


if play_again == "yes":
hangman()

else:
print("Thanks for playing!")
hangman()
Output:

You might also like