I'm a hobbist trying to become a professional software developer. I do have a lot interest in Data Science in general and my goal is to master Python before diging deeper into specific data science topics.
I created this game when I started studying object oriented programming. Thank you all in advance.
import os
import getpass
class Hangman:
winner = False
word = ''
mask = ''
mistakes = []
def __init__(self, word):
self.word = word
self.mask = '_ ' * len(word)
def make_move(self, letter):
letter_match = self.word.find(letter)
if letter_match >= 0:
for i, w in enumerate(self.word):
if w == letter:
new_mask = list(self.mask)
new_mask[i * 2] = letter
self.mask = ''.join(new_mask)
self.check_winner()
else:
self.mistakes.append(letter)
print('Wrong!')
def check_winner(self):
if self.mask.find('_') < 0:
self.show_board()
print('You won!')
self.winner = True
return self.winner
def check_looser(self):
if len(self.mistakes) > 4:
print('You loose! The word was %s' % self.word)
quit()
def show_board(self):
os.system('cls')
print(self.mask)
print(self.mistakes)
def play(self):
while not self.winner:
self.show_board()
letter_input = input('Choose a letter: ').upper()
self.make_move(letter_input)
self.check_looser()
secret_input = getpass.getpass('Secret Word: ').upper()
game = Hangman(secret_input)
game.play()