Hangman game using Python

Hangman game

Hangman game is one of the oldest game played by the 90’s kid. They were very popular at that time and mostly played by the kids and the teenagers.
In this game, we have used some old materials like asking questions and entering some of the answers. The game is very simple as the users enter the game and he/she has to give the name and the game will start.
You have ten chance to guess the answer or you will lose the game. If you guess it right then it will show YOU WON or if you didn’t give the right answer till the last chance then you will lose the game.
The game begins like this:

[smartslider3 slider=60]
Like you can see from the above slide, what the game actuallly looks like.
Things you need to install to run this project:
  1. You need to install python 2.7 or python 3.
  2. You need to install python library like time.
  3.  sudo pip / pip3 install time
#importing the time module
import time
#welcoming the user
name = raw_input("What is your name? ")
print "Hello, " + name, "Time to play hangman!"
print "
"
#wait for 1 second
time.sleep(1)
print "Start guessing..."
time.sleep(0.5)
#here we set the secret
word = "secret"
#creates an variable with an empty value
guesses = ''
#determine the number of turns
turns = 10
# 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"
    # exit the script
        break
    print
    # ask the user go guess a character
    guess = raw_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 (now 9)
        turns -= 1
    # print wrong
        print "Wrong
"
    # how many turns are left
        print "You have", + turns, 'more guesses'
    # if the turns are equal to zero
        if turns == 0:
        # print "You Loose"
            print "You Loose
"  

Click on below download button to run this project.
Download
Thank you,

You may also like...

Leave a Reply

avatar
  Subscribe  
Notify of