Logo 
Search:

Python Articles

Submit Article
Home » Articles » Python » Programming BasicsRSS Feeds

Hangman Game

Posted By: Falak Khan     Category: Python     Views: 12782

Code for Hangman Game in Python

import random
######################################################


def get_any(category):
    return random.sample(category, 1)[0]


#################################################################333


def list_of_alphabets(already_tried):
    alphabets=[chr(i) for i in range(ord('a'), ord('z')+1)]
    d = list(set(alphabets) - set(already_tried))
    d.sort()
    print"list of alphabets is :", d

def get_entities(fn):
    return open('animals.txt').read().split()



########################################################

## RETURNS GUESSED WORD ##

def guess(word,masked_word,c):
    to_return=""for i in range(len(word)):
        if(word[i]==c):
            to_return=to_return+c
        else:
            to_return=to_return+masked_word[i]
    return to_return


##########################################################


def mask(word):
    return"-" * len(word)



########################################################3



## INPUT GUESS LETTER FROM USER ##

def get_guess(already_tried):
   
    c=str(raw_input("Enter your next guess:  "))
  
    while(c in already_tried):
        print "you have already entered this, Enter another one "
        c=str(raw_input("Enter your next guess:  "))
    return c
    
        


########################################################



## INTERFACE ##

def draw_interface(missed,mask_word,guessed, already_tried):
    draw_hangman(missed)
    print "Guessed: ",guessed
    print mask_word
    list_of_alphabets(already_tried)
        

###########################################################

   
## HANGMAN IMAGES ##

def draw_hangman(tri):
    hangman=["""
              ############
              ##         !
              ##          
              ##          
              ##          
              ##          
              ##          
              ##          
            =======""","""

              ############
              ##         ! 
              ##       (@_@)
              ##
              ##
              ##
              ##    
              ##
              ##
           ========""","""

              ############
              ##         ! 
              ##       (@_@)
              ##        ( )
              ##       
              ##
              ##     
              ##
              ##
           ========""","""

            ############
            ##         ! 
            ##       (@_@)
            ##       <( )>
            ##       
            ##   
            ##    
            ##
            ##
         ========""","""

           ############
           ##         ! 
           ##       (~_~)
           ##       <( )>
           ##       _/ \_
           ##        
           ##     HANGED !!!
           ## 
           ##
        ========"""]
    
    print hangman[tri]
    print 4-tri,"tries left"


###################################################################
        
## PROGRAM BODY ##
animals=['elephant',"monkey","owl","peacock","Tigress"]
fruits=["apple","orange","peach","banana","watermelon"]
country=["pakistan","england","iran","america","australia"]
city=["islamabad","sydney","london","carmel","lahore"]
wtp=True 
while(wtp):
    already_tried = []
    print "choose a category : "
    category=str(raw_input(" Animal=a , Fruit=f , Country=c , city=t \n"))
    if(category=="a"):
        word = get_any(animals)
    elif(category=="f"):
        word=get_any(fruits)
    elif(category=="c"):
        word="pakistan"else:
        word="london"
    missed=0
    mask_word=mask(word)
    guessed=[ ]
    while(missed<=4):
        draw_interface(missed,mask_word,guessed, already_tried)
        if(missed!=4):
            c=get_guess(already_tried)
            already_tried.append(c)
        if(word.count(c)>0):
            mask_word=guess(word,mask_word,c)
        else:
            guessed.append(c)
            missed=missed+1
        if(word==mask_word):
            print "     YOU WON !!!"break
        print word
    choice=str(raw_input("would you like to play again y/n :"))
    while(choice!='y' and choice!='n'):
        choice=str(raw_input("PLEASE ENTER 'y/n' ONLY :  "))
    if(choice=="n"):
        wtp=False

#####################################################################
        

    
  
Share: 

 
 

Didn't find what you were looking for? Find more on Hangman Game Or get search suggestion and latest updates.

Falak Khan
Falak Khan author of Hangman Game is from Pakistan.
 
View All Articles

 

Other Interesting Articles in Python:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Abhishek Singh from India Comment on: May 13
good yaar.............

View All Comments