Hangman
is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other(s) tries to guess it by suggesting letters or numbers, within a certain number of guesses.
The word to guess is represented by a row of dashes, representing each letter of the word. In most variants, proper nouns, such as names, places, and brands, are not allowed. Slang words, sometimes referred to as informal or shortened words, are also not allowed.
If the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. If the suggested letter does not occur in the word, the other player draws one element of a hanged man stick figure as a tally mark.
The player guessing the word may, at any time, attempt to guess the whole word. If the word is correct, the game is over and the guesser wins. Otherwise, the other player may choose to penalize the guesser by adding an element to the diagram.
On the other hand, if the other player makes enough incorrect guesses to allow his opponent to complete the diagram, the game is also over, this time with the guesser losing. However, the guesser can also win by guessing all the letters or numbers that appears in the word, thereby completing the word, before the diagram is completed.
The above image shows an example game of Hangman in progress. The underlined letters appear in the word in their correct places, while the crossed-out letters do not appear, and each crossed-out letter corresponds to one part of the drawing. In this case, the secret word is “hangman”.
Approach to solve the game
In this game, we set a secret word.
The guesser first has to input his name, and then, will be asked to guess any alphabet.
If the random word contains that alphabet, it will be shown as the output (with correct placement) else the program will ask you to guess another alphabet.
Guesser will be given length_of_word+2 turns (can be changed accordingly) to guess the complete word.
If the guesser guesses all the letters in the word within the number of turns then he wins the game else loses the game.
def play_hangman():
name = input("What is your name? ")
print(f"Hello {name}, Time to play hangman!")
print("Start guessing...")
print("HINT: word is the name of a fruit\n")
# here we set the secret word
word = "apple"
# creates an variable with an empty value
guesses = ""
# determine the number of turns
turns = len(word) + 2
# Create a while loop
# check if the turns are more than zero
while turns > 0:
# make a counter that starts with zero
failed = 0
# for every character in secret_word
for char in word:
# see if the character is in the players guess
if char in guesses:
# print then out the character
print(char)
else:
# if not found, print a dash
print("_")
# and increase the failed counter with one
failed += 1
# if failed is equal to zero
# print You Won
if failed == 0:
print("You won the game :-)")
# exit the script
break
# ask the user go guess a character
guess = input("Guess a character: ")
# set the players guess to guesses
guesses += guess
# if the guess is not found in the secret word
if guess not in word:
# turns counter decreases with 1
turns -= 1
# print wrong
print("Wrong guess")
# how many turns are left
print(f"You have {turns} more guesses")
# if the turns are equal to zero
if turns == 0:
# print "You Loose"
print("You lost the game :-(")
if __name__ == "__main__":
play_hangman()
What is your name? Babloo Hello Babloo, Time to play hangman! Start guessing... HINT: word is the name of a fruit _ _ _ _ _ Guess a character: z Wrong guess You have 6 more guesses _ _ _ _ _ Guess a character: a a _ _ _ _ Guess a character: r Wrong guess You have 5 more guesses a _ _ _ _ Guess a character: p a p p _ _ Guess a character: q Wrong guess You have 4 more guesses a p p _ _ Guess a character: p a p p _ _ Guess a character: c Wrong guess You have 3 more guesses a p p _ _ Guess a character: e a p p _ e Guess a character: j Wrong guess You have 2 more guesses a p p _ e Guess a character: l a p p l e You won the game :-)