Hangman Word Guessing Game

Choose a random word from a list
and
display underscores for each letter in the word.
The user can guess one letter at a time,
and
if they guess a correct letter,
the program should fill in all instances of that letter in the word.
If they guess an incorrect letter,
the program should keep track of the number of incorrect guesses
and
display a hangman figure.

import random
Word_Bracket = ['you','statistics','satisfaction','purification','hot','goat',
                'amsterdam','hustle','catfish','lamb','western','question','carnivour',
                'lost','ghosting','drought','zeal','now','tip']

state = True
guesses = 0

# to display wrong guessed letters so player can avoid guessing them again
wrong_letters =[]

# selecting random word for the game
ans = Word_Bracket[random.randint(0,len(Word_Bracket)-1)]

'''
creating encryption for the user
using '.' instead '_' as underscores merge with each other
'''

s = '.'*len(ans)

'''
creating dictionary for storing letters of the word
and its positions so that we can reveal all occurences at once
'''
d = {}
for i in range(len(ans)):
    # if it is first occurence add letter as key and its position as value
    if ans[i] in d.keys():
        d[ans[i]].append(i)
    else:
    # if already discovered letter add position to the list of letter as key
        d[ans[i]] = [i]

# main code
while(state):
    print('Guess letter of the word:')
    print(s)
    letter = input()

    if letter in ans:
        '''
        if letter is in keys of d replace all the '.'s with letter at positions in 
        list of occurences of the letter i.e. d[letter]
        '''
        
        s = [letter if i in d[letter] else s[i] for i in range(len(s))]
        s = ''.join(s)
        if s == ans:
            print('Congratulations !!!!')
            print(f'You completed the word **{s}**')
            print(f'Total wrong guesse : {guesses}')
            state = False
    else:
        wrong_letters.append(letter)
        print('Wrong letter !!!')
        print(f'Wrong letters : {wrong_letters} ')
        guesses +=1
        print(f'Wrong guesses: {guesses}')
Guess letter of the word:
........
Guess letter of the word:
.....i..
Wrong letter !!!
Wrong letters : ['a'] 
Wrong guesses: 1
Guess letter of the word:
.....i..
Guess letter of the word:
..e..i..
Guess letter of the word:
..e..io.
Guess letter of the word:
..e..ion
Guess letter of the word:
..e.tion
Guess letter of the word:
..estion
Wrong letter !!!
Wrong letters : ['a', 'g'] 
Wrong guesses: 2
Guess letter of the word:
..estion
Guess letter of the word:
.uestion
Congratulations !!!!
You completed the word **question**
Total wrong guesse : 2
2 Likes

@kharshavardhan31
Very good.
Keep sharing :+1:

1 Like

This is so cool @kharshavardhan31, keep growing!! :grin: