Skip to content

Instantly share code, notes, and snippets.

@reloadedd
Created December 23, 2019 11:05
Show Gist options
  • Select an option

  • Save reloadedd/78e40c5f73de89cf46d063b1028f2bc7 to your computer and use it in GitHub Desktop.

Select an option

Save reloadedd/78e40c5f73de89cf46d063b1028f2bc7 to your computer and use it in GitHub Desktop.

Revisions

  1. reloadedd created this gist Dec 23, 2019.
    56 changes: 56 additions & 0 deletions 32_hangman.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    #!/usr/bin/env python3

    '''
    Contents of Practice_Python.functions:
    from random import choice
    # Used for Hangman series
    def pick_random_word() -> str:
    """Pick a random number from a list of words."""
    with open('sowpods.txt') as file:
    wordlist = file.readlines()
    return choice(wordlist)
    '''

    from Practice_Python.functions import pick_random_word

    WORD = pick_random_word().strip() # Remove all whitespace

    if __name__ == '__main__':
    print(WORD)
    guesses = 0
    build_word = list('_' * len(WORD))

    print('>>> Welcome to Hangman! You have 6 guessed. Good luck!')
    print('_ ' * len(WORD))

    while guesses < 6:
    guess = input('>>> Guess your letter: ').upper() # Just to make sure
    while len(guess) != 1:
    guess = input('>>> Guess your letter: ').upper()

    if guess in WORD:
    i = 0
    for letter in WORD:
    if guess == letter:
    if build_word[i] == letter:
    print('Already guessed. Try another letter.')
    # guesses -= 1
    break
    else:
    build_word[i] = letter
    i += 1

    print(' '.join(build_word))

    if '_' not in build_word:
    print('Hooray! You guessed the word!')
    break # End the game
    else:
    print('Incorrect!')
    guesses += 1

    print('Guesses left: ', (6 - guesses))