rajeevs1992

Hangman

Jun 21st, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. #hangman
  2. import random
  3. pics=[' ',
  4.     '''
  5. =========||
  6.    |    ||
  7.         ||
  8.         ||
  9.         ||
  10.         ||
  11.         || ''',
  12. '''
  13. =========||
  14.    |    ||
  15.    0    ||
  16.         ||
  17.         ||
  18.         ||
  19.         || ''',
  20. '''
  21. =========||
  22.    |    ||
  23.    0    ||
  24.    |    ||
  25.         ||
  26.         ||
  27.         || ''',
  28. '''
  29. =========||
  30.    |    ||
  31.    0    ||
  32.   /|    ||
  33.         ||
  34.         ||
  35.         || ''',
  36. '''
  37. =========||
  38.    |    ||
  39.    0    ||
  40.   /|\  ||
  41.         ||
  42.         ||
  43.         || ''',
  44. '''
  45. =========||
  46.    |    ||
  47.    0    ||
  48.   /|\  ||
  49.   /     ||
  50.         ||
  51.         || ''',
  52. '''
  53. =========||
  54.    |    ||
  55.    0    ||
  56.   /|\  ||
  57.   / \  ||
  58.         ||
  59.         || ''',
  60. ]
  61. f1=open("twl06.txt","r")
  62. word=f1.read()
  63. word=word.lower()
  64. word=word.split()
  65. def getWord(WordList):
  66.     Index=random.randint(0,len(WordList)-1)
  67.     return WordList[Index]
  68. def display(Correct,Missed,SecretWord):
  69.     print (pics[len(Missed)])
  70.     print("Here is the list of letters you missed ")
  71.     for Letter in Missed+Correct:
  72.         print (Letter,end=' ')
  73.     Blanks='_'*len(SecretWord)
  74.     for i in range(len(SecretWord)):
  75.         if SecretWord[i] in Correct:
  76.             Blanks=Blanks[:i]+SecretWord[i]+Blanks[i+1:]
  77.     print ("\nHere is the secret word...\n")
  78.     print("The word is ",end='')
  79.     print(len(SecretWord),end=' ')
  80.     print("characters long")
  81.     for i in range (0,len(Blanks)):
  82.         print (Blanks[i],end=' ')
  83.     print()
  84.     ch=''
  85.     if Blanks == SecretWord:
  86.         print("Congrats!!!!Word guessed!!!")
  87.         print("Play again???y/n ")
  88.         ch=input()
  89.         guess(ch)
  90.     elif Blanks !=SecretWord and len(Missed)==7:
  91.         print("You lose\n")
  92.         print ("The word was ",end='')
  93.         print(SecretWord)        
  94.         print("Play again???y/n ")
  95.         ch=input()
  96.         guess(ch)        
  97. def guess(Choice):
  98.     if Choice=='y':
  99.         print('Welcome to HANGMAN!\n')
  100.         Missed=''
  101.         Correct=''
  102.         SecretWord=getWord(word)
  103.         print('_ '*len(SecretWord))
  104.         while len(Missed)<len(pics)-1 :
  105.             print('Please guess a letter...\n')
  106.             letter=input()
  107.             flag=checkLetter(letter,Missed,Correct)
  108.             if flag == 1 and letter in SecretWord:
  109.                 Correct=Correct+letter
  110.                 display(Correct,Missed,SecretWord)
  111.             elif flag==1 and letter not in SecretWord:
  112.                 Missed=Missed+letter
  113.                 display(Correct,Missed,SecretWord)
  114.     else:
  115.         exit("\nthank you for playing")
  116. def checkLetter(letter,Missed,Correct):
  117.     UnForbidden='abcdefghijklmnopqrstuvwxyz'
  118.     if letter in UnForbidden and len(letter) == 1 and letter not in Missed+Correct:
  119.         return 1
  120.     elif letter not in UnForbidden:
  121.         print("Enter only alphabets\n")
  122.         return 2
  123.     elif len(letter) !=1:
  124.         print("Only one character allowed\n")
  125.         return 3
  126.     elif letter in Missed+Correct:
  127.         print("Already used\n")
  128.         return 4
  129. guess('y')
Advertisement
Add Comment
Please, Sign In to add comment