Still struggling with list population based off of input and comparison. Any help would be appreciated

I am still struggling to implement a helper function showing possible matches in a list based off of previous user input (accumulated guess).

The full code at the bottom of this post works well for the base game. It just seems maybe I have coded a nice end result for the base game without it being workable code when trying to implement certain functions the way I know how.

When implementing this helper function listed below (def(match_with_gaps)), I struggle to have a good list to cross reference my wordlist against due to the fact that my “correct_guess” list repopulates on every calling of the “get_guessed_word” function. I have it doing this instead of defining it in the global scope of the program because it adds duplicates to the output if not defined and the start of the function and just throws off the output in the base game.

I have tried this to accomplish implementing the “match_with_gaps” function many different ways including. Defining “correct_guess” in the global scope. Cloning “correct_guess” as a new list, populating an existing empty list with what’s contained in correct_guess at the end of the function call etc etc etc.

Lists/Strings - “my_word”(attempting to take from “correct_guess”), “other_word” (iterating over the wordlist to check for possible matches), and “correct_guess”(letters or dashes joined to form the current guess), seem to be the issue when implementing the new function. Although correct_guess works fine in the base game. I’m just seriously struggling to find a way to implement this.

The random portion of this code is commented out to use the word ‘apple’ for testing.

The Functional Game code listed below will work without the wordlist as the random portion is commented out.

If the wordlist is needed to help me troubleshoot the helper function. It is from MIT OCW 6.0001 I can send it. Or any random wordlist you can acquire should be sufficient.

Any advice/help/comments based off the code listed below would be very helpful, I’ve been stuck on this for awhile.

Desired Function

def match_with_gaps(my_word, other_word):
    '''
    my_word: string with _ characters, current guess of secret word
    other_word: string, regular English word
    returns: boolean, True if all the actual letters of my_word match the 
        corresponding letters of other_word, or the letter is the special symbol
        _ , and my_word and other_word are of the same length;
        False otherwise: 
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass

def show_possible_matches(my_word):
    '''
    my_word: string with _ characters, current guess of secret word
    returns: nothing, but should print out every word in wordlist that matches my_word
             Keep in mind that in hangman when a letter is guessed, all the positions
             at which that letter occurs in the secret word are revealed.
             Therefore, the hidden letter(_ ) cannot be one of the letters in the word
             that has already been revealed.

Functional Game Below

import random
import string
letters_guessed = []



WORDLIST_FILENAME = "words.txt"


def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist



def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)
    
    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code

# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = load_words()


def is_word_guessed(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; assumes all letters are
      lowercase
    letters_guessed: list (of letters), which letters have been guessed so far;
     assumes that all letters are lowercase
    returns: boolean, True if all the letters of secret_word are in letters_guessed;
     False otherwise#
    '''
    ## FILL IN YOUR CODE HERE AND DELETE "pass"
    for e in secret_word:
        if e not in letters_guessed:
            return True
    print("You Have guessed the secret word!")
    return False

        
            

            
            
                  
            
            
            


    
   



def get_guessed_word(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    correct_guess = []
    for e in secret_word:
        if e in letters_guessed:
            correct_guess.append(e)
        else:
            correct_guess.append('_ ')
    print(''.join(correct_guess))
            
            
            
    
            
            
            
        
            
            
            
            
            
        
            
            
           

            
def get_available_letters(letters_guessed):
    '''
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    for e in available_letters:
        if e in letters_guessed:
            available_letters.remove(e)
    print(("Available Letters: "),' '.join(available_letters))
    
    

def hangman(secret_word):
    '''
    secret_word: string, the secret word to guess.
    
    Starts up an interactive game of Hangman.
    
    * At the start of the game, let the user know how many 
      letters the secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!
    
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    num_guesses = 6
    num_warning = 3
    unique_char = set()
    print("Welcome to the game Hangman!")
    print("I am thinking of a word that is", len(secret_word), "letters long.")
    print("You have", num_guesses,"guesses left.")
    while is_word_guessed(secret_word, letters_guessed):
        if num_guesses != 0:
            player_turn = input("Enter Guess: ")
            letters_guessed.append(player_turn)
            for l in player_turn:
                if l not in available_letters:
                    num_warning -=1
                    print("You may only enter lowercase alphabetic letters that have not already ")
                    get_available_letters(letters_guessed)
                    get_guessed_word(secret_word, letters_guessed)
                    if num_warning <= 0:
                        num_guesses -= 1
                        print("You have 0 warnings remaining")
                        print("You have", num_guesses,"guesses left.")
                        break
                    else:
                        print("You have",num_warning,"warnings remaining")
                        print("You have", num_guesses,"guesses left.")
                elif player_turn not in secret_word:
                    num_guesses -= 1
                    get_available_letters(letters_guessed)
                    get_guessed_word(secret_word, letters_guessed)
                    print("You have", num_guesses,"guesses left.")
                    print("I'm sorry, that letter was not in the Secret Word.")
                else: 
                    get_available_letters(letters_guessed)
                    get_guessed_word(secret_word, letters_guessed)
                    print("Good Guess!")
                    print("You have", num_guesses,"guesses left.")
                
                for character in secret_word:
                    unique_char.add(character)
                
            continue
        print("I'm sorry you have lost!")
        print("The secret word was",secret_word)
        break
    score = len(unique_char)*num_guesses
    print("Your score is",score)  
    
    
    
    
    



# When you've completed your hangman function, scroll down to the bottom
# of the file and uncomment the first two lines to test
#(hint: you might want to pick your own
# secret_word while you're doing your own testing)


# -----------------------------------



def match_with_gaps(my_word, other_word):
    '''
    my_word: string with _ characters, current guess of secret word
    other_word: string, regular English word
    returns: boolean, True if all the actual letters of my_word match the 
        corresponding letters of other_word, or the letter is the special symbol
        _ , and my_word and other_word are of the same length;
        False otherwise: 
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



def show_possible_matches(my_word):
    '''
    my_word: string with _ characters, current guess of secret word
    returns: nothing, but should print out every word in wordlist that matches my_word
             Keep in mind that in hangman when a letter is guessed, all the positions
             at which that letter occurs in the secret word are revealed.
             Therefore, the hidden letter(_ ) cannot be one of the letters in the word
             that has already been revealed.

    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



def hangman_with_hints(secret_word):
    '''
    secret_word: string, the secret word to guess.
    
    Starts up an interactive game of Hangman.
    
    * At the start of the game, let the user know how many 
      letters the secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses
    
    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Make sure to check that the user guesses a letter
      
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
      
    * If the guess is the symbol *, print out all words in wordlist that
      matches the current guessed word. 
    
    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



# When you've completed your hangman_with_hint function, comment the two similar
# lines above that were used to run the hangman function, and then uncomment
# these two lines and run this file to test!
# Hint: You might want to pick your own secret_word while you're testing.



if __name__ == "__main__":
    # pass

    # To test part 2, comment out the pass line above and
    # uncomment the following two lines.
    available_letters = string.ascii_lowercase
    available_letters = list(available_letters[:])
    #secret_word = choose_word(wordlist)
    secret_word = 'apple'
    hangman(secret_word)

    
###############
    
    # To test part 3 re-comment out the above lines and 
    # uncomment the following two lines. 
    
    #secret_word = choose_word(wordlist)
    #hangman_with_hints(secret_word)

An attempt at implemented desired helper function
It at least populates the possible_matches list, although it is checking against “secret_word”, instead of “my_word”. And is only populating possible matches based off of len(), not len()/actual letter.

import random
import string
letters_guessed = []
my_word = []
special_characters = "*"
possible_matches = []
other_word = []


WORDLIST_FILENAME = "words.txt"


def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist



def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)
    
    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code

# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = load_words()


def is_word_guessed(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; assumes all letters are
      lowercase
    letters_guessed: list (of letters), which letters have been guessed so far;
     assumes that all letters are lowercase
    returns: boolean, True if all the letters of secret_word are in letters_guessed;
     False otherwise#
    '''
    ## FILL IN YOUR CODE HERE AND DELETE "pass"
    for e in secret_word:
        if e not in letters_guessed:
            return True
    print("You Have guessed the secret word!")
    return False

        
            

            
            
                  
            
            
            


    
   



def get_guessed_word(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    correct_guess = []
    for e in secret_word:
        if e in letters_guessed:
            correct_guess.append(e)
        else:
            correct_guess.append('_ ')
    print(''.join(correct_guess))
    my_word = correct_guess[:]
    (''.join(my_word))
     
            
            
            
    
            
            
        
            
            
           

            
def get_available_letters(letters_guessed):
    '''
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    for e in available_letters:
        if e in letters_guessed:
            available_letters.remove(e)
    print(("Available Letters: "),' '.join(available_letters))
    
    

# def hangman(secret_word):
#     '''
#     secret_word: string, the secret word to guess.
    
#     Starts up an interactive game of Hangman.
    
#     * At the start of the game, let the user know how many 
#       letters the secret_word contains and how many guesses s/he starts with.
      
#     * The user should start with 6 guesses

#     * Before each round, you should display to the user how many guesses
#       s/he has left and the letters that the user has not yet guessed.
    
#     * Ask the user to supply one guess per round. Remember to make
#       sure that the user puts in a letter!
    
#     * The user should receive feedback immediately after each guess 
#       about whether their guess appears in the computer's word.

#     * After each guess, you should display to the user the 
#       partially guessed word so far.

#     Follows the other limitations detailed in the problem write-up.
#     '''
#     # FILL IN YOUR CODE HERE AND DELETE "pass"
#     num_guesses = 6
#     num_warning = 3
#     unique_char = set()
#     print("Welcome to the game Hangman!")
#     print("I am thinking of a word that is", len(secret_word), "letters long.")
#     print("You have", num_guesses,"guesses left.")
#     while is_word_guessed(secret_word, letters_guessed):
#         if num_guesses != 0:
#             player_turn = input("Enter Guess: ")
#             letters_guessed.append(player_turn)
#             for l in player_turn:
#                 if l not in available_letters:
#                     num_warning -=1
#                     print("You may only enter lowercase alphabetic letters that have not already ")
#                     get_available_letters(letters_guessed)
#                     get_guessed_word(secret_word, letters_guessed)
#                     if num_warning <= 0:
#                         num_guesses -= 1
#                         print("You have 0 warnings remaining")
#                         print("You have", num_guesses,"guesses left.")
#                         break
#                     else:
#                         print("You have",num_warning,"warnings remaining")
#                         print("You have", num_guesses,"guesses left.")
#                 elif player_turn not in secret_word:
#                     num_guesses -= 1
#                     get_available_letters(letters_guessed)
#                     get_guessed_word(secret_word, letters_guessed)
#                     print("You have", num_guesses,"guesses left.")
#                     print("I'm sorry, that letter was not in the Secret Word.")
#                 else: 
#                     get_available_letters(letters_guessed)
#                     get_guessed_word(secret_word, letters_guessed)
#                     print("Good Guess!")
#                     print("You have", num_guesses,"guesses left.")
                
#                 for character in secret_word:
#                     unique_char.add(character)
                
#             continue
#         print("I'm sorry you have lost!")
#         print("The secret word was",secret_word)
#         break
#     score = len(unique_char)*num_guesses
#     print("Your score is",score)  
    
    
    
    
    



# When you've completed your hangman function, scroll down to the bottom
# of the file and uncomment the first two lines to test
#(hint: you might want to pick your own
# secret_word while you're doing your own testing)


# -----------------------------------



def match_with_gaps(my_word, other_word):
    '''
    my_word: string with _ characters, current guess of secret word
    other_word: string, regular English word
    returns: boolean, True if all the actual letters of my_word match the 
        corresponding letters of other_word, or the letter is the special symbol
        _ , and my_word and other_word are of the same length;
        False otherwise: 
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    for other_word in wordlist:
        for letter in other_word:
            if len(other_word) == len(secret_word):
                if letter in secret_word:
                    possible_matches.append(other_word)
                    True
    False
        



def show_possible_matches(my_word):
    '''
    my_word: string with _ characters, current guess of secret word
    returns: nothing, but should print out every word in wordlist that matches my_word
             Keep in mind that in hangman when a letter is guessed, all the positions
             at which that letter occurs in the secret word are revealed.
             Therefore, the hidden letter(_ ) cannot be one of the letters in the word
             that has already been revealed.

    '''
    pass



def hangman_with_hints(secret_word):
    '''
    secret_word: string, the secret word to guess.
    
    Starts up an interactive game of Hangman.
    
    * At the start of the game, let the user know how many 
      letters the secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses
    
    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Make sure to check that the user guesses a letter
      
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
      
    * If the guess is the symbol *, print out all words in wordlist that
      matches the current guessed word. 
    
    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    num_guesses = 6
    num_warning = 3
    unique_char = set()
    print("Welcome to the game Hangman!")
    print("I am thinking of a word that is", len(secret_word), "letters long.")
    print("You have", num_guesses,"guesses left.")
    while is_word_guessed(secret_word, letters_guessed):
        if num_guesses != 0:
            player_turn = input("Enter Guess: ")
            letters_guessed.append(player_turn)
            for l in player_turn:
                if l not in available_letters:
                    if l not in special_characters:
                        num_warning -=1
                        print("You may only enter lowercase alphabetic letters that have not already ")
                        get_available_letters(letters_guessed)
                        get_guessed_word(secret_word, letters_guessed)
                    else:
                        match_with_gaps(my_word, other_word)
                       
                    if num_warning <= 0:
                        num_guesses -= 1
                        print("You have 0 warnings remaining")
                        print("You have", num_guesses,"guesses left.")
                        break
                    else:
                        print("You have",num_warning,"warnings remaining")
                        print("You have", num_guesses,"guesses left.")
                elif player_turn not in secret_word:
                    num_guesses -= 1
                    get_available_letters(letters_guessed)
                    get_guessed_word(secret_word, letters_guessed)
                    print("You have", num_guesses,"guesses left.")
                    print("I'm sorry, that letter was not in the Secret Word.")
                else: 
                    get_available_letters(letters_guessed)
                    get_guessed_word(secret_word, letters_guessed)
                    print("Good Guess!")
                    print("You have", num_guesses,"guesses left.")
                
                for character in secret_word:
                    unique_char.add(character)
                
            continue
        print("I'm sorry you have lost!")
        print("The secret word was",secret_word)
        break
    score = len(unique_char)*num_guesses
    print("Your score is",score)  
    



# When you've completed your hangman_with_hint function, comment the two similar
# lines above that were used to run the hangman function, and then uncomment
# these two lines and run this file to test!
# Hint: You might want to pick your own secret_word while you're testing.



if __name__ == "__main__":
    # pass

    # To test part 2, comment out the pass line above and
    # uncomment the following two lines.
    available_letters = string.ascii_lowercase
    available_letters = list(available_letters[:])
    #secret_word = choose_word(wordlist)
    secret_word = 'apple'
    #hangman(secret_word)

    
###############
    
    # To test part 3 re-comment out the above lines and 
    # uncomment the following two lines. 
    
    #secret_word = choose_word(wordlist)
    hangman_with_hints(secret_word)