Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. from typing import Set
  2. import random
  3.  
  4. MAX_GUESS = 6
  5. HARD_LEVEL = {'easy': 50, 'middle': 70, 'advanced': 100}
  6. RAND_HINT = True
  7. DIC_COW = {
  8. 6: r"""
  9. ^__^
  10. (oo)\_______
  11. (__)\ )\/\
  12. ||----w |
  13. || ||
  14. """,
  15. 5: r"""
  16. (oo)\_______
  17. (__)\ )\/\
  18. ||----w |
  19. || ||
  20. """,
  21. 4: r"""
  22. \_______
  23. \ )\/\
  24. ||----w |
  25. || ||
  26. """,
  27. 3: r"""
  28. \_______
  29. \ )\/\
  30. ----w |
  31. ||
  32. """,
  33. 2: r"""
  34. \_______
  35. \ )\/\
  36. ----w
  37.  
  38. """,
  39. 1: r"""
  40. \_______
  41. \ )
  42. ----w
  43.  
  44. """,
  45. 0: r"""
  46.  
  47.  
  48.  
  49.  
  50. """
  51. }
  52.  
  53. def readDic() -> str:
  54. pass
  55.  
  56. def guessDic(word: str) -> None:
  57. if word is None or len(word) == 0:
  58. return
  59.  
  60. global MAX_GUESS
  61.  
  62. guess_set = set()
  63. word_set = set(word)
  64. guess_count = 0
  65.  
  66. if RAND_HINT:
  67. rand_pick = random.choices(list(word_set), k = random.randrange(HARD_LEVEL['easy'] * len(word_set) // 100, len(word_set)))
  68. for c in rand_pick:
  69. guess_set.add(c)
  70.  
  71. print('Welcome to Hangman!')
  72. printLeft(MAX_GUESS - guess_count)
  73. printDic(word, guess_set)
  74.  
  75. while guess_count < MAX_GUESS:
  76. while True:
  77. s = input('Guess your letter: ')
  78. if s.isalpha() and len(s) == 1:
  79. break
  80.  
  81. if s in guess_set:
  82. print('Letter {} has been guessed!'.format(s))
  83. continue
  84. elif s not in guess_set and s in word_set:
  85. guess_set.add(s)
  86. guess_count += 1
  87. elif s not in guess_set and s not in word_set:
  88. guess_set.add(s)
  89. guess_count += 1
  90. print('Incorrect!')
  91.  
  92. printDic(word, guess_set)
  93.  
  94. if word_set.issubset(guess_set):
  95. print('Congratulation, you have make it after {} times try!'.format(guess_count))
  96. break
  97.  
  98. printLeft(MAX_GUESS - guess_count)
  99.  
  100. def printDic(word: str, guess_set: Set) -> None:
  101. l = []
  102. for c in word:
  103. if c not in guess_set:
  104. l.append('_')
  105. else:
  106. l.append(c)
  107. print(' '.join(l))
  108.  
  109. def printLeft(guess_count: int) -> None:
  110. print(DIC_COW[guess_count])
  111. print('You have {} incorrect guesses left!'.format(guess_count))
  112.  
  113. def main():
  114. words = ['hello', 'will', 'hiccup', 'fun', 'tiny', 'shock', 'water' \
  115. 'drink', 'quick', 'hopping']
  116.  
  117. while True:
  118. guessDic(random.choice(words))
  119. i = ''
  120.  
  121. while True:
  122. i = str(input('Do you want to play again (Y/y/N/n)? '))
  123. if i in ['Y', 'y', 'N', 'n']:
  124. break
  125. if i in ['Y', 'y']:
  126. continue
  127. elif i in ['N', 'n']:
  128. break
  129.  
  130. if __name__ == '__main__':
  131. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement