Skip to content

Instantly share code, notes, and snippets.

@scarolan
Created October 3, 2023 20:22
Show Gist options
  • Save scarolan/bae64152e9be3b45e2119686abaccdf6 to your computer and use it in GitHub Desktop.
Save scarolan/bae64152e9be3b45e2119686abaccdf6 to your computer and use it in GitHub Desktop.

Revisions

  1. scarolan created this gist Oct 3, 2023.
    132 changes: 132 additions & 0 deletions hangman.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    import random
    WORD_LIST = ['APPLE', 'BANANA', 'CHERRY', 'DONKEY', 'ELEPHANT',
    'FLAMINGO', 'GORILLA', 'HIPPO', 'IGUANA', 'JAGUAR',
    'KANGAROO', 'LEMUR', 'MONKEY', 'NYALA', 'OSTRICH',
    'PENGUIN', 'QUOKKA', 'RABBIT', 'SQUIRREL', 'TURTLE',
    'UGUISU', 'VULTURE', 'WALRUS', 'XERUS', 'YAK', 'ZEBRA']
    HANGMAN_PICS = [
    '''
    +---+
    |
    |
    |
    ===
    ''',
    '''
    +---+
    O |
    |
    |
    ===
    ''',
    '''
    +---+
    O |
    | |
    |
    ===
    ''',
    '''
    +---+
    O |
    /| |
    |
    ===
    ''',
    '''
    +---+
    O |
    /|\ |
    |
    ===
    ''',
    '''
    +---+
    O |
    /|\ |
    / |
    ===
    ''',
    '''
    +---+
    O |
    /|\ |
    / \ |
    ===
    '''
    ]
    TAUNTS = [
    "Wrong guess... looks like you got a little a-head of yourself. πŸ’€",
    "Another wrong letter, added a body there! You seem to be falling apart! πŸͺ“",
    "Oh no, an arm appeared! You're really going out on a limb here. πŸͺ΅",
    "A second arm, huh? Talk about being up in arms! πŸ€·πŸ»β€β™€οΈ",
    "And there's a leg! Guess you don't have a leg to stand on now. 🦡",
    "My, my, looks like you're on your last leg. Better step it up! πŸ˜‰"
    ]
    def get_random_word(word_list):
    word_index = random.randint(0, len(word_list) - 1)
    return word_list[word_index]
    def display_board(hangman_pics, missed_letters, correct_letters, secret_word):
    print(hangman_pics[len(missed_letters)])
    print()
    print('Missed letters:', end=' ')
    for letter in missed_letters:
    print(letter, end=' ')
    print()
    blanks = '_' * len(secret_word)
    for i in range(len(secret_word)):
    if secret_word[i] in correct_letters:
    blanks = blanks[:i] + secret_word[i] + blanks[i+1:]
    for letter in blanks:
    print(letter, end=' ')
    print()
    def get_guess(already_guessed):
    while True:
    print('Guess a letter.')
    guess = input()
    guess = guess.upper()
    if len(guess) != 1:
    print('Please enter a single letter.')
    elif guess in already_guessed:
    print('You have already guessed that letter. Choose again.')
    elif not guess.isalpha():
    print('Please enter a LETTER.')
    else:
    return guess
    def play_again():
    print('Do you want to play again? (yes or no)')
    return input().lower().startswith('y')
    print('H A N G M A N')
    missed_letters = ''
    correct_letters = ''
    secret_word = get_random_word(WORD_LIST)
    game_is_done = False
    while True:
    display_board(HANGMAN_PICS, missed_letters, correct_letters, secret_word)
    guess = get_guess(missed_letters + correct_letters)
    if guess in secret_word:
    correct_letters = correct_letters + guess
    found_all_letters = True
    for i in range(len(secret_word)):
    if secret_word[i] not in correct_letters:
    found_all_letters = False
    break
    if found_all_letters:
    print('Yes! The secret word is "' + secret_word + '"! You have won!')
    game_is_done = True
    else:
    missed_letters = missed_letters + guess
    print(TAUNTS[len(missed_letters) - 1]) # Taunt the user
    if len(missed_letters) == len(HANGMAN_PICS) - 1:
    display_board(HANGMAN_PICS, missed_letters, correct_letters, secret_word)
    print('You have run out of guesses! After ' + str(len(missed_letters)) + ' missed guesses and ' + str(len(correct_letters)) + ' correct guesses, the word was "' + secret_word + '"')
    game_is_done = True

    if game_is_done:
    if play_again():
    missed_letters = ''
    correct_letters = ''
    game_is_done = False
    secret_word = get_random_word(WORD_LIST)
    else:
    break