Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- _GOWN_MODIFIER = {
- 1: (' O ', 2),
- 2: (' | ', 3),
- 3: ('/| ', 3),
- 4: ('/|\\', 3),
- 5: ('/ ', 4),
- 6: ('/ \\', 4),
- }
- SECRET_WORD = 'Hangman'.upper()
- ALPHABET = list(string.ascii_uppercase)
- HEIGHT = 7
- WIDTH = 13
- def insert_correct_guess(guess, temp_secret_word):
- for index, letter in enumerate(SECRET_WORD):
- if letter == guess:
- temp_secret_word[index] = letter
- return temp_secret_word
- def hangman_game():
- temp_secret_word = ['_']*len(SECRET_WORD)
- guessed_letters = []
- can_guess = False
- for gown in wrong_answer(WIDTH, HEIGHT):
- if not can_guess:
- print gown
- while can_guess:
- print
- print ' '.join(temp_secret_word)
- print
- print ' Guessed: ' + ' '.join(guessed_letters)
- guess = manage_user_input(temp_secret_word)
- guessed_letters.append(guess)
- if guess not in SECRET_WORD:
- print '\n You guessed incorrectly cowboy!'
- break
- print '\n You guessed correctly cowboy!'
- print gown
- temp_secret_word = insert_correct_guess(guess, temp_secret_word)
- if ''.join(temp_secret_word) == SECRET_WORD:
- print ' You won cowboy! '
- return
- if can_guess:
- print gown
- can_guess = True
- print ' You died cowboy! '
- return
- def manage_user_input(guessed_letters):
- valid_guess = False
- while not valid_guess:
- guess = raw_input(' Guess a letter cowboy! : ').upper()
- if guess not in ALPHABET:
- print 'That is not in the alphabet cowboy, please try again!'
- elif guess in guessed_letters:
- print 'You have already tried to guess', guess, 'cowboy, please try again!'
- else:
- valid_guess = True
- return guess
- def wrong_answer(width, height):
- gown = create_gown(width, height)
- half_width = width // 2
- yield '\n'.join(gown)
- for attempt in range(1, 7):
- pattern, row = _GOWN_MODIFIER[attempt]
- gown[row] = '{}{}{}{}'.format(' '*(INDENT-1), pattern, ' '*(half_width-len(pattern)+1), '|')
- yield '\n'.join(gown)
- INDENT = 3
- def create_gown(width, height):
- half_width = width // 2
- gown = ['{}{}{}'.format(' '*INDENT, ' '*half_width, '|')]*height
- gown[0] = '{}{}'.format(' '*INDENT, '_' * (half_width+1))
- gown[1] = '{}{}{}{}'.format(' '*INDENT, '|',' '*(half_width-1), '|')
- gown[-1] = '{}{}'.format(' '*INDENT, '-' * width)
- return gown
- if __name__ == '__main__':
- hangman_game()
Advertisement
Add Comment
Please, Sign In to add comment