# Improvements to simple Hangman Game in Python

I created this hangman game today as my first stand alone project. It is a quite basic and simplified approach. How could I improve the game to make it more interesting?

    import random
,"backfields", "blueprints", "chivalrous", "complexity", "cyberpunks", "desolating",
"duplicator","earthlings",
"fluoridate","flustering","fornicates","godfathers","humanizers","ideographs","journalism",
"languished","logarithms","mendacious","neighborly","outlandish","palindrome","philanders"
,"proclaimed","randomizes","shrinkable","sublimated",

i = random.randint(0, len(words) - 1)

word = words[i]
guesses = "_"*len(word)
print(guesses)
lives = 0
while lives < 5:
character_check = input("")
if character_check in word and character_check != word:
a = word.index(character_check)
guesses = guesses[:a] + word[a] + guesses[a+1:]
print(guesses)
if guesses == word or character_check == word:
print("Winner!")
break
elif character_check not in word:
print(lives)
lives += 1
if lives == 5:
print("You lose! It was {}".format(word))


This is a great start. Let me point out a few things you should consider.

## Make it interactive

When I ran the code, it produces some numbers whenever I lose a guess, it took a while to figure out that it was my guess count incrementing. A nice prompt message would be helpful. Also the guess count should reduce for each guess made.

## Avoid hardcorded values

What is 5 in the following statement while lives < 5:. it can be written as follow

MAX_LIVES = 5
while lives <= MAX_LIVES:


character_check is just too misleading, promises one thing(to check a character) and does another thing(recieves input from the user). This can rewritten as user_input

1. You could make the game a class,implement a board class to nicely draw the man being hung.
2. You could also implement a hint package interface, user might want to buy and use hints when they are stuck.
• Thanks! I have since changed a couple things already. I did notice afterwards that the lives should have been decreasing not increasing, I had like a D'oh moment. I like the idea for hints, I'll try to incorporate that. Nov 19, 2020 at 19:13

A couple of coding suggestions...

1. You can use random.choice(words) to pick a random item out of the words list.

2. I'd be tempted to sys.exit() instead of break on winning. That way you don't need to test the value of lives - if you reach the end of the while loop you have lost.

In terms of making the program 'more interesting', I'd add a welcome message, maybe explain the rules. Maybe also display a list of letters already guessed and do nothing if the same letter is guessed twice? (Right now choosing the same letter 5times loses the game). you could also have short, medium and long (or easy, medium, hard) word lists and ask the person to choose which they want to use.

• Thanks! I'll try to implement those things as soon as possible Nov 19, 2020 at 11:38