Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class hangman():
- def __init__(self, word):
- self.word = list(word)
- self.print_word = ["-"] * len(word)
- self.correct_number = 0
- self.hp = 8
- def test_letter(self, letter):
- count = 0
- if letter in self.word:
- for i in range(len(self.word)):
- if self.word[i] == letter:
- self.print_word[i] = letter
- count += 1
- self.correct_number += count
- if count == 0:
- self.hp -= 1
- return count
- def get_print_str(self):
- return self.print_word
- def get_hp(self):
- return self.hp
- def check_win(self):
- return self.correct_number == len(self.word)
- word_list = ["python", "pascal", "javascript", "delphi"]
- game = hangman(random.choice(word_list))
- print("H A N G M A N")
- used_letters = set()
- while True:
- print(*game.get_print_str(), sep="")
- s = input("\n\nInput a letter: > ")
- if not (len(s) == 1 and s.isalpha()):
- print("Incorrect input try again")
- continue
- if "A" <= s <= "Z":
- s = chr(ord(s) - 97 - 65)
- if s in used_letters:
- print(s, "уже было использовано")
- continue
- else:
- used_letters.add(s)
- if "a" <= s <= "z":
- answer = game.test_letter(s)
- if answer:
- print("Вы отгадали", answer, "букв")
- else:
- print("Не угадали")
- if game.get_hp():
- print("Осталось", game.get_hp(), "попыток")
- else:
- print("Вь проиграли")
- input("Введите enter чтобы выйти")
- continue
- else:
- print("Incorrect input try again")
- continue
- if game.check_win():
- print(game.get_print_str())
- print("You guessed the word!\nYou survived!")
- input("Press enter to exit")
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement