Advertisement
andewK

Untitled

Dec 2nd, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. import random
  2.  
  3. class hangman():
  4.     def __init__(self, word):
  5.         self.word = list(word)
  6.         self.print_word = ["-"] * len(word)
  7.         self.correct_number = 0
  8.         self.hp = 8
  9.    
  10.     def test_letter(self, letter):
  11.         count = 0
  12.         if letter in self.word:
  13.           for i in range(len(self.word)):
  14.             if self.word[i] == letter:
  15.               self.print_word[i] = letter
  16.               count += 1
  17.         self.correct_number += count
  18.         if count == 0:
  19.           self.hp -= 1
  20.         return count
  21.  
  22.     def get_print_str(self):
  23.         return self.print_word
  24.  
  25.     def get_hp(self):
  26.         return self.hp
  27.  
  28.     def check_win(self):
  29.         return self.correct_number == len(self.word)
  30.  
  31. word_list = ["python", "pascal", "javascript", "delphi"]
  32.  
  33. game = hangman(random.choice(word_list))
  34.  
  35. print("H A N G M A N")
  36.  
  37. used_letters = set()
  38.  
  39. while True:
  40.   print(*game.get_print_str(), sep="")
  41.   s = input("\n\nInput a letter: > ")
  42.   if not (len(s) == 1 and s.isalpha()):
  43.     print("Incorrect input try again")
  44.     continue
  45.  
  46.   if "A" <= s <= "Z":
  47.     s = chr(ord(s) - 97 - 65)
  48.  
  49.   if s in used_letters:
  50.     print(s, "уже было использовано")
  51.     continue
  52.   else:
  53.     used_letters.add(s)
  54.  
  55.   if "a" <= s <= "z":
  56.     answer = game.test_letter(s)
  57.     if answer:
  58.       print("Вы отгадали", answer, "букв")
  59.     else:
  60.       print("Не угадали")
  61.       if game.get_hp():
  62.         print("Осталось", game.get_hp(), "попыток")
  63.       else:
  64.         print("Вь проиграли")
  65.         input("Введите enter чтобы выйти")
  66.       continue
  67.  
  68.   else:
  69.     print("Incorrect input try again")
  70.     continue
  71.  
  72.   if game.check_win():
  73.     print(game.get_print_str())
  74.     print("You guessed the word!\nYou survived!")
  75.     input("Press enter to exit")
  76.     exit(0)
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement