hangman with ten chances So this is the code I wrote for hangman in python. I want to know of can shorten this code more further and make it look more professional

# importing random function
import random, hanged
from word_meaning import meanings

# Dictionary of random words
word_dict = '''whistle,
park,
picayune,
attach,
grubby,
gusty,
neck,
necessary,
day,
compete,
memory,
interfere,
sophisticated,
lumpy,
spiky,
frail'''.split(',\n')


# System chooses a random word
rand_word = word_dict[random.randint(0, len(word_dict)-1)]

print('Welcome to HANGMAN.\nGuess correct letters to complete the hidden word, Else hangman will be Hanged')



n = len(rand_word)
hidden_word = ((n-1)*'*')
hidden_word_1 = hidden_word.split('*')
print(f'Hidden Word: {hidden_word}*')
print(f'Hint: {meanings[str(rand_word)]}')
#This is the dictionary imported from another file. it gives the meaning of the hidden word as a hint

chances = 10


def user_lose():
    global chances
    print(f'Oops wrong guess\n Chances Left : {chances - 1}')
    if chances &gt; 1:
        chances -= 1
        user_guess()
    else:
        hanged.hangman()
#hanged.Hangman() this is the function which shows pic of hangman. imported from another file

def user_guess():
    global chances
    if '' in hidden_word_1:
        user_input = str(input('\nGuess a letter: ')[0]).lower()
        if user_input in rand_word:
            if user_input in hidden_word_1:
                user_lose()
            else:
                if chances &gt; 0:
                    letter_index = rand_word.index(user_input)
                    letter_index_r = rand_word.rindex(user_input)
                    hidden_word_1[letter_index] = user_input
                    hidden_word_1[letter_index_r] = user_input
                    print(hidden_word_1)
                    user_guess()

        else:
            user_lose()

    else:
        print('Hurrah, You saved Hangman')


user_guess()


hanged.py file

def hangman():
    print('''You could not save hangman\n
                                   -------------------
                                   |         |
                                   |         |
                                   |         |
                                   |      ( ` ` ) 
                                   |         |
                                   |        /|\\
                                   |       / | \\
                                   |         |  
                                   |        / \\ 
                                   |       /   \\
                                   |
                                   |
                                   |
                                   |                 ''')
I don't have word_meaning, is that another dependency ?

Since you are already using the random module, you can simply select a random element from a list with random.choice like this:

rand_word = random.choice(word_dict)


instead of:

rand_word = word_dict[random.randint(0, len(word_dict)-1)]


Here you are picking only one element at a time, but if you wanted to select several unique elements (no repeating items) then you would use random.sample.



Your list of words is a constant, make it so:

word_dict = (
'attach', 'compete', 'day', 'frail', 'grubby', 'gusty', 'interfere', 'lumpy', 'memory',
'necessary', 'neck', 'park', 'picayune', 'sophisticated', 'spiky', 'whistle'
)


(did a simple search &amp; replace in Notepad++). NB: I have sorted it too.

The way you are splitting the string is awkward and the comma delimiter is superfluous. This design choice forces you to waste one line per word and makes the code longer for no benefit.

Note that I made it a tuple (non mutable) instead of a list, hence the parentheses.

You could also load the list from a text file, if you want flexibility and separate data from code.



It is not safe to rely on \n as a line break delimiter because it is platform-specific. On Windows/Mac it is different although Python should still interpret it the same. But you never know. Think about portability issues.

Relying on global variables is probably not so Pythonic. You could pass chances as a parameter to your functions. And if you make changes to that value, then return the value in your functions.

Using a global variable could make debugging harder because the variable can be altered in so many places. If you refactor your functions at some point the variable could even be out of sync.



I would aim for minimal variable scope. If hidden_word_1 (not a great name) is only used in user_guess then it need no be defined as a global variable.



I cannot run your code right now but I am not convinced you need a recursive function. You simply need a while loop eg while chances &gt; 0: and decrement the counter after each wrong response.