hangman-game.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from os import system
  2. word_list = ["monkey", "mouse", "camel"]
  3. #TODO-1 - Randomly choose a word from the word_list and assign it to a variable called chosen_word.
  4. #TODO-2 - Ask the user to guess a letter and assign their answer to a variable called guess. Make guess lowercase.
  5. #TODO-3 - Check if the letter the user guessed (guess) is one of the letters in the chosen_word.
  6. import random
  7. game_word = random.choice(word_list)
  8. display = []
  9. for i in range(len(game_word)):
  10. display.append("_")
  11. def print_result():
  12. result = "||" + "||".join(display) + "||"
  13. print(result)
  14. def change_display(positions):
  15. if len(positions) > 0:
  16. for pos in positions:
  17. display[pos] = game_word[pos]
  18. life_counter = 6
  19. def print_ascii_art():
  20. stages = ['''
  21. +---+
  22. | |
  23. O |
  24. /|\ |
  25. / \ |
  26. |
  27. =========
  28. ''', '''
  29. +---+
  30. | |
  31. O |
  32. /|\ |
  33. / |
  34. |
  35. =========
  36. ''', '''
  37. +---+
  38. | |
  39. O |
  40. /|\ |
  41. |
  42. |
  43. =========
  44. ''', '''
  45. +---+
  46. | |
  47. O |
  48. /| |
  49. |
  50. |
  51. =========''', '''
  52. +---+
  53. | |
  54. O |
  55. | |
  56. |
  57. |
  58. =========
  59. ''', '''
  60. +---+
  61. | |
  62. O |
  63. |
  64. |
  65. |
  66. =========
  67. ''', '''
  68. +---+
  69. | |
  70. |
  71. |
  72. |
  73. |
  74. =========
  75. ''']
  76. print(stages[life_counter])
  77. def check_letter(letter):
  78. right_letters = []
  79. for n in range(len(game_word)):
  80. if game_word[n] == letter:
  81. right_letters.append(n)
  82. return right_letters
  83. def check_word():
  84. is_complete = True
  85. if "_" in display:
  86. is_complete = False
  87. return is_complete
  88. inputed_letters = []
  89. system('clear')
  90. print_result()
  91. while True:
  92. guess = input("Guess a letter: ").lower()
  93. system('clear')
  94. if guess not in inputed_letters:
  95. inputed_letters.append(guess)
  96. results = check_letter(guess)
  97. if len(results) > 0:
  98. change_display(results)
  99. print("Yes takaya bukva")
  100. else:
  101. life_counter -= 1
  102. print("No matches")
  103. else:
  104. print("This letter already opened. Try another one")
  105. print_result()
  106. print_ascii_art()
  107. if check_word():
  108. print("You are win!")
  109. break
  110. if life_counter < 1:
  111. print("You are dead")
  112. break