Python – Beginner – Intermediate hangman project, cool

Another great python project from previous same video project 5. Hangman 24:25 – 35:28, this one is a bit harder than the guess a number one. There was some bugs with the code from the video that may have been intentional, I have notated and made changes so my code will have some changes plus modifications. I really like this completed game, its pretty tough to guess the words and the data you get back helps a lot.

Here is my modified code below, everything is functional and a couple more stats were added to show lives remaining if you get an answer wrong. You need a file with list of words to make this work named words.py in same directory as this file hangman.py with just this one variable with big list of words like, words = [‘about’, ‘alley’, ‘ more’] if you watch the video you can see the source of the huge word list she uses. If that word list isn’t made right this script will not work.


# Updated some messages 4/29/2022

import random
from words import words
import string

def get_valid_word(words):
    word = random.choice(words) #randomly chooses something from list
    while '-' in word or ' ' in word:
        word = random.choice(words)
    return word

def hangman():
    word = get_valid_word(words)
    word_letters = set(word) # letters in word
    alphabet = set(string.ascii_lowercase) #bug check lowercase instead of uppercase
    used_letters = set() # what the user has guessed

    lives = 10 # player lives

    while len(word_letters) > 0 and lives > 0:
        print('You have used these letters: ', ' '.join(used_letters).upper()) # modified to upper display
        word_list = [letter if letter in used_letters else '-' for letter in word]
        print('Current word: ', ' '.join(word_list).upper()) # modified to upper display

        # getting user input
        user_letter = input(f'{lives} lives remaining. Guess a letter: ').lower() #bug check from upper to lower, added lives number
        if user_letter in alphabet - used_letters: 
            used_letters.add(user_letter)
            if user_letter in word_letters: 
                word_letters.remove(user_letter)

            else:
                lives -= 1
                print('That letter is not in the word.')

        elif user_letter in used_letters:
            print('You have already used that letter, try again.')
        else:
            print('Invalid letter, try again.')

    if lives == 0:
        print('You ran out of lives, game over. The word was', word.upper() )
    else:
        print('You guessed the correct word', word.upper())

hangman()

# Sample game play text below.

You have used these letters:  
Current word:  - - - - -
10 lives remaining. Guess a letter: a
Letter is not in the word.
You have used these letters:  A
Current word:  - - - - -
9 lives remaining. Guess a letter: u
Letter is not in the word.
You have used these letters:  U A
Current word:  - - - - -
8 lives remaining. Guess a letter: j
Letter is not in the word.
You have used these letters:  U A J
Current word:  - - - - -
7 lives remaining. Guess a letter: h
You have used these letters:  H U A J
Current word:  - H - - -
7 lives remaining. Guess a letter: g
You have used these letters:  H A G J U
Current word:  G H - - -
7 lives remaining. Guess a letter: t
You have used these letters:  H A G J T U
Current word:  G H - - T
7 lives remaining. Guess a letter: e
Letter is not in the word.
You have used these letters:  H A G J T U E
Current word:  G H - - T
6 lives remaining. Guess a letter: d
Letter is not in the word.
You have used these letters:  D H A G J T U E
Current word:  G H - - T
5 lives remaining. Guess a letter: o
You have used these letters:  D H A G J O T U E
Current word:  G H O - T
5 lives remaining. Guess a letter: s
You guessed the correct word ghost !!

Leave a Comment