Guest User

Untitled

a guest
Feb 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import random
  2.  
  3. #Function that choose the word from the .txt
  4. def ChooseWord(file):
  5. with open(file,"r") as f:
  6. lines=f.readlines()
  7. lines=[i.replace("\n","") for i in lines]
  8. return random.sample(lines,1)
  9.  
  10. #Function that ask you for a letter and check if its correct
  11. #+ count incorrect tries
  12.  
  13. def AsknCheck(word,guessword,used_letter):
  14. while True:
  15.  
  16. letter=input("Guess your letter:")
  17. letter=letter.upper()
  18. if letter in used_letter:
  19. print("You guessed this letter already.")
  20.  
  21. elif letter in word:
  22. print("Guess letter: " +letter)
  23. used_letter.add(letter)
  24. return letter
  25. break
  26. else:
  27. print("Incorrect!")
  28. used_letter.add(letter)
  29. return 0
  30.  
  31.  
  32. #Function that complements the word when you guessed correctly.
  33. def WriteWord(guessword,word,letter):
  34. for i in range(len(word)):
  35. if word[i]==letter:
  36. guessword[i]=letter
  37. elif guessword[i]!="-":
  38. guessword[i]=guessword[i]
  39. else:
  40. guessword[i]="-"
  41. return guessword
  42.  
  43. #Function that is creating "-----" from the word at the very beginning.
  44. def Initialization(word):
  45. startword=[]
  46. for i in range(len(word)):
  47. startword.append("-")
  48. return startword
  49.  
  50. #Function for drawing hangman.
  51. def DrawHANGMAN(count):
  52. top="--------\n| |\n| |"
  53. mid={0: '|',
  54. 1: "| O",
  55. 2: "| O\n| /",
  56. 3: "| O\n| /|",
  57. 4: "| O\n| /|\\",
  58. 5: "| O\n| /|\\\n| /",
  59. 6: "| O\n| /|\\\n| / \\"}
  60. mid1="|\n|\n|\n|"
  61. bottom="---"
  62.  
  63. print(top)
  64. print(mid[count])
  65. print(mid1)
  66. print(bottom)
  67.  
  68.  
  69.  
  70. if __name__ == '__main__':
  71. ans=""
  72. while ans!="quit":
  73. word="".join(ChooseWord("sowpods.txt")) ## Random word from the .txt
  74. guessword=Initialization(word)
  75. used_letter=set()
  76. print("Welcome to HANGMAN. Please guess letter.")
  77. count=0
  78. DrawHANGMAN(count)
  79. while True:
  80. letter=AsknCheck(word,guessword,used_letter)
  81. if letter == 0:
  82. count+=1
  83. left=6-count
  84. DrawHANGMAN(count)
  85. print("You have " +str(left) +" incorrect guesses left")
  86. guessword=WriteWord(guessword,word,letter)
  87. if "".join(guessword)==word:
  88. print("You won't be hanged today.")
  89. break
  90. elif count==6:
  91. print("You have been hanged!")
  92. break
  93. print("".join(guessword))
  94. ans=input("If you want to end type:'quit'.")
Add Comment
Please, Sign In to add comment