Replacing a specific value in a dictionary

So I’m not entirely sure what issue you are experiencing, I’m guessing it’s because calling print(mwodict.values().replace('*',l)) doesn’t actually ‘save state’ per se (Also .values() returns an iterator which doesn’t have a .replace() method so I’m guessing it’s throwing an AttributeError). Nothing updates the dictionary to keep track of what letters have been guessed and which haven’t.
I’m not certain this is what you want, but what if I recommend a different approach? Create a list of the letters in the word (letters_list), and create a list of *'s in the word (star_list). Prompt the user to guess a letter, if the letter is in the letters_list: trade that letter for a star in the star_list. Continue until the user is out of tries or has guessed the word (in other words, letters_list has replaces star_list).

So if the word is “example”, and the user guesses "e’:
letters_list becomes ['*', 'x', 'a', 'm', 'p', 'l', '*']
while star_list becomes ['e', '*', '*', '*', '*', '*', 'e']

def guessing_game(word, chances):
    letters = list(word)  # ['w', 'o', 'r', 'd']
    guess = ['*' for x in word]  # ['*', '*', '*', '*']
    rounds = list(range(chances))
    rounds.reverse()  # [7,6,5,4,3,2,1,0]
    for r in rounds:
        print(f'\nWord: {"".join(guess)}')
        g = input('Guess Letter: ')[0]
        # Find the index of all characters in 'letters' that match g
        # Ensures User only has to guess duplicate characters once
        idxs = [i for i, x in enumerate(letters) if x == g]
        # Swap *'s and letters for all i's in idxs
        for i in idxs:
            letters[i], guess[i] = guess[i], letters[i]
        if '*' not in guess:
            print(f'\nYou Guessed It: {word}!')
            return True
        if r is not 0: print(f'{r} Chances Remaining')
    print(f'Sorry, the word was: {word}')
    return False


if __name__ == '__main__':
    words = ['rose','camion','partie','sifflet','coureur','fleur']
    chances = 8
    guessing_game(choice(words), chances)
    while True:
        cont = input('Keep Playing? (Y)es/(N)o: ')
        if cont[0].lower() == 'y': guessing_game(choice(words), chances)
        else: break
    sys.exit()

This gives you a lot of flexibility to building your game. The function itself returns a True/False value so you can use that in win/lose logic like score counting. The word choices can easily be expanded upon (or retrieved from the web), and an option to change difficulty (number of chances) is there. It also just seems much easier than using a dictionary as the main data structure (unless that’s what you needed).

1 Like