Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- HANGMAN_PICS = ['''
- +---+
- |
- |
- |
- ===''', '''
- +---+
- O |
- |
- |
- ===''', '''
- +---+
- O |
- | |
- |
- ===''', '''
- +---+
- O |
- /| |
- |
- ===''', '''
- +---+
- O |
- /|\ |
- |
- ===''', '''
- +---+
- O |
- /|\ |
- / |
- ===''', '''
- +---+
- O |
- /|\ |
- / \ |
- ===''']
- words = '''
- ant duck muck tart fart panda bear train cat dog pan egg supercalifragilisticexpialidocious '''.split()
- def get_random_word(word_list):
- word_index = random.randint(0, len(word_list) - 1)
- return word_list[word_index]
- def display_board(missed_let, correct_let, secret_word):
- print(HANGMAN_PICS[len(missed_let)])
- print()
- print('Missed letters:', end=' ')
- for letter in missed_let:
- print(letter, end=' ')
- print()
- blanks = '_' * len(secret_word)
- for i in range(len(secret_word)):
- if secret_word[i] in correct_let:
- blanks = blanks[:i] + secret_word[i] + blanks[i+1:]
- for letter in blanks:
- print(letter, end=' ')
- print()
- def get_guess(already_guessed):
- while True:
- guess = input('guess a letter')
- guess = guess.lower()
- if len(guess) != 1:
- print('please enter in a single LETTER')
- elif guess in already_guessed:
- print('you already guessed that')
- elif guess not in 'abcdefghijklmnopqrstuvwyxz':
- print('please enter a LETTER')
- else:
- return guess
- def play_again():
- return input('Do you wantr to play again? (yes or no').lower().startswith('y')
- print('H A N G M A N')
- missed_let = ''
- correct_let = ''
- secret_word = get_random_word(words)
- game_is_done = False
- while True:
- display_board(missed_let, correct_let, secret_word)
- guess = get_guess(missed_let + correct_let)
- if guess in secret_word:
- correct_let = correct_let + guess
- found_all_letters = True
- for i in range(len(secret_word)):
- if secret_word[i] not in correct_let:
- found_all_letters = False
- break
- if found_all_letters:
- print('Yes the sercret word is' + secret_word + 'You won!')
- game_is_done = True
- else:
- missed_let = missed_let + guess
- if len(missed_let) == len(HANGMAN_PICS) - 1:
- display_board(missed_let,correct_let,secret_word)
- print('YOu have ruyn out of guess \n' + str(len(missed_let)) + 'correct guess'
- + str(len(correct_let)) + 'correct guess, the word was' + secret_word)
- game_is_done = True
- if game_is_done:
- if play_again():
- missed_let = ''
- correct_let = ''
- game_is_done = False
- secret_word = get_random_word(words)
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment