# Game of Hangman Using Python Program

Computing Using Python: Write a program that plays the game of hangman.Use characters to print the hangman’s status. Triple-quoted strings will be useful. Hint: draw the entire hangman status as a string picture with a full picture for each partial hangman status.

Solution:

```import random
board = [
'  +---+   n  |   |   n      |   n      |   n      |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n      |   n      |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n  |   |   n      |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n /|   |   n      |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n /|\  |   n      |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n /|\  |   n /    |   n      |   n========= n',
'  +---+   n  |   |   n  0   |   n /|\  |   n / \  |   n      |   n========= n'
]
class Hangman:
def __init__(self,word):
self.word = word
self.missed_letters = []
self.guessed_letters = []
def guess(self,letter):
if letter in self.word and letter not in self.guessed_letters:
self.guessed_letters.append(letter)
elif letter not in self.word and letter not in self.missed_letters:
self.missed_letters.append(letter)
else:
return False
return True
def hangman_over(self):
return self.hangman_won() or (len(self.missed_letters) == 6)
def hangman_won(self):
if '_' not in self.hide_word():
return True
return False
def hide_word(self):
rtn = ''
for letter in self.word:
if letter not in self.guessed_letters:
rtn += '_'
else:
rtn += letter
return rtn
def print_game_status(self):
print board[len(self.missed_letters)]
print 'Word: ' + self.hide_word()
print 'Letters Missed: ',
for letter in self.missed_letters:
print letter,
print
print 'Letters Guessed: ',
for letter in self.guessed_letters:
print letter,
print
def rand_word():
return bank[random.randint(0,len(bank))]
def main():
game = Hangman(rand_word())
while not game.hangman_over():
game.print_game_status()
user_input = raw_input('nEnter a letter: ')
game.guess(user_input)
game.print_game_status()
if game.hangman_won():
print 'nCongratulations! You are the winner of Hangman!'
else:
print 'nSorry, you have lost in the game of Hangman...'
print 'The word was ' + game.word
print 'nGoodbye!n'
if __name__ == "__main__":
main()
```

• Rasha

this is a very good website

• maani

I have 50 questions for the same test your page is showing only 28

• joeanne

• joeanne

hi can anyone help or guide me to my assignments. thanks

• Monik

• Cristina

This solution is perfect ...thanks

• Janete

Hello Allison,I love the 2nd image that you did! I also, had never heard of SumoPaint, is something that I will have to exolpre a bit! I understand completely the 52 (or so) youtube videos that you probably watched. Sometimes they have what you want, sometimes they don't! However, it is always satisfying when you are able to produce something that you have taught yourself. Great job!Debra 0 likes

• Sandeep

Perfect bank of solution.

• Oxana

great !

• Paul Brandon-Fritzius

thanks for the quick response. the solution looks good. :)

• tina Johnson

thnx for the answer. it was perfect. just the way i wanted it.

• Giuseppe

works fine.