Replacing a specific value in a dictionary

Below a part of the program I am writing, I have difficulty to make the last part function correctly.
mwodict is a dictionary where the keys are the letters of a word chosen randomly with choice method, and the values are *
The user should type a letter to try guessing the hidden word.
l is the input() letter, in case the letter typed is one of the hidden word letters, the program should return the values ‘x’ of the word except the value of l which should be replacing it’s corresponding value’
For example:
The mwodict for the word “sifflet” should appear like this:
{'s': '*', 'i': '*', 'f': '*', 'l': '*', 'e': '*', 't': '*'}
If the user types the letter I , then my program should print as follows:
{'s': '*', 'i': **'i'**, 'f': '*', 'l': '*', 'e': '*', 't': '*'}
But the last part (print (mwodict.values().replace(’
’,l))) is not returning what is expected
(Note the letter f is listed only once in the dictionary which is another issue I am trying to correct)

ty/// Variables ///
/// Program ///
while k<8:
    l = input("Please type a letter: \n")
    lslettres = list(string.ascii_letters)
    if l not in lslettres:
             print("You didn't type a letter! \n")
     elif l in mwodict.keys():
                print (mwodict.values().replace('*',l))
                k+=1pe or paste code here


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

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

Thanks a lot for your feedback, yes you are right my code will result in a AttributeError.
I will use this approach with list nstead of dictionary, in fact in the beginning I started testing the program with lists but I was stuck and didn’t thought of trading the letters with stars in the way you illustrated.