Hangman Game With Python

Branzolde
3 min readJul 4, 2020

--

make a simple hangman game with Python programming language

Hello Every one, Today I am going to show you how to make a simple hangman game with Python programming language.

The Plan

Our hangman game has to do the following:

Let the user guess characters for random words

Now you can start thinking about how this might work in code. The code will need to do the following:

  1. Use the random library
  2. make an input variable for the user name and say hi
  3. make a list of words
  4. create a variable that randomly choose words from out previous list
  5. tell the user to guess the characters
  6. create a guesses variable
  7. create a turns variable and set it to any number
  8. create a while loop for turns while greater than 0
  9. create a variable in the while loop and call it failed and it equals to 0
  10. create a for loop inside the while loop (for char in word)
  11. create an if statement in the for loop (if char in guesses) then print it, else print “_” and increase the failed variable by one
  12. create an if statement inside the while loop for failed and set it to 0 and if its true print you win and print the word and break the while loop.
  13. create an input ion the guess variable to guess the character
  14. store every input character in the guesses variable
  15. create an if statement if guesses not in word then reduce “turns “by one and print wrong and print the number of turns left for the user
  16. if turns = 0 you lose
import random 
# library that we use in order to choose
# on random words from a list of words

name = input("What is your name? ")
# Here the user is asked to enter the name first

print("Good Luck ! ", name)

words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']

# Function will choose one random
# word from this list of words
word = random.choice(words)


print("Guess the characters")

guesses = ''

# any number of turns can be used here
turns = 12


while turns > 0:

# counts the number of times a user fails
failed = 0

# all characters from the input
# word taking one at a time.
for char in word:

# comparing that character with
# the character in guesses
if char in guesses:
print(char)

else:
print("_")

# for every failure 1 will be
# incremented in failure
failed += 1


if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("You Win")

# this print the correct word
print("The word is: ", word)
break

# if user has input the wrong alphabet then
# it will ask user to enter another alphabet
guess = input("guess a character:")

# every input character will be stored in guesses
guesses += guess

# check input with the character in word
if guess not in word:

turns -= 1

# if the character doesn’t match the word
# then “Wrong” will be given as output
print("Wrong")

# this will print the number of
# turns left for the user
print("You have", + turns, 'more guesses')


if turns == 0:
print("You Loose")

Output:

What is your name? Gautam
Good Luck! Gautam
Guess the characters
_
_
_
_
_
guess a character:g
g
_
_
_
_
guess a character:e
g
e
e
_
_
guess a character:k
g
e
e
k
_
guess a character:s
g
e
e
k
s
You Win
The word is: geeks

Conclusion

Hope you learned something new about programming and problem solving and stay tuned for more.

--

--