Karsol

Untitled

Apr 2nd, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import random
  2.  
  3. HANGMAN_PICS = ['''
  4. +---+
  5. |
  6. |
  7. |
  8. ===''', '''
  9. +---+
  10. O |
  11. |
  12. |
  13. ===''', '''
  14. +---+
  15. O |
  16. | |
  17. |
  18. ===''', '''
  19. +---+
  20. O |
  21. /| |
  22. |
  23. ===''', '''
  24. +---+
  25. O |
  26. /|\ |
  27. |
  28. ===''', '''
  29. +---+
  30. O |
  31. /|\ |
  32. / |
  33. ===''', '''
  34. +---+
  35. O |
  36. /|\ |
  37. / \ |
  38. ===''']
  39.  
  40. words = '''
  41. ant duck muck tart fart panda bear train cat dog pan egg supercalifragilisticexpialidocious '''.split()
  42.  
  43. def get_random_word(word_list):
  44. word_index = random.randint(0, len(word_list) - 1)
  45. return word_list[word_index]
  46.  
  47. def display_board(missed_let, correct_let, secret_word):
  48. print(HANGMAN_PICS[len(missed_let)])
  49. print()
  50.  
  51. print('Missed letters:', end=' ')
  52. for letter in missed_let:
  53. print(letter, end=' ')
  54. print()
  55.  
  56. blanks = '_' * len(secret_word)
  57.  
  58. for i in range(len(secret_word)):
  59. if secret_word[i] in correct_let:
  60. blanks = blanks[:i] + secret_word[i] + blanks[i+1:]
  61. for letter in blanks:
  62. print(letter, end=' ')
  63. print()
  64. def get_guess(already_guessed):
  65. while True:
  66. guess = input('guess a letter')
  67. guess = guess.lower()
  68. if len(guess) != 1:
  69. print('please enter in a single LETTER')
  70. elif guess in already_guessed:
  71. print('you already guessed that')
  72. elif guess not in 'abcdefghijklmnopqrstuvwyxz':
  73. print('please enter a LETTER')
  74. else:
  75. return guess
  76.  
  77. def play_again():
  78. return input('Do you wantr to play again? (yes or no').lower().startswith('y')
  79.  
  80. print('H A N G M A N')
  81. missed_let = ''
  82. correct_let = ''
  83. secret_word = get_random_word(words)
  84. game_is_done = False
  85.  
  86. while True:
  87. display_board(missed_let, correct_let, secret_word)
  88.  
  89. guess = get_guess(missed_let + correct_let)
  90.  
  91. if guess in secret_word:
  92. correct_let = correct_let + guess
  93.  
  94. found_all_letters = True
  95. for i in range(len(secret_word)):
  96. if secret_word[i] not in correct_let:
  97. found_all_letters = False
  98. break
  99. if found_all_letters:
  100. print('Yes the sercret word is' + secret_word + 'You won!')
  101. game_is_done = True
  102. else:
  103. missed_let = missed_let + guess
  104.  
  105. if len(missed_let) == len(HANGMAN_PICS) - 1:
  106. display_board(missed_let,correct_let,secret_word)
  107. print('YOu have ruyn out of guess \n' + str(len(missed_let)) + 'correct guess'
  108. + str(len(correct_let)) + 'correct guess, the word was' + secret_word)
  109. game_is_done = True
  110. if game_is_done:
  111. if play_again():
  112. missed_let = ''
  113. correct_let = ''
  114. game_is_done = False
  115. secret_word = get_random_word(words)
  116. else:
  117. break
Advertisement
Add Comment
Please, Sign In to add comment