Hangman Game in Python 3

Peachy Software logo


Below is a little hangman game I wrote back in 2018 for fun, please feel free to download and use the code as you like.

You will need to download the words_alpha.txt file from
here, and place it in your game .py directory for this to work, as the file contains all the words that can be selected at random for the game to work!

import random
import time

word_selection = (random.choice(
open('words_alpha.txt').read().split()).strip())
#welcoming the user
print("\n**** Welcome to Hangman created by Grant Peach 2018 ****")
name =
input("\nWhat is your name? \n")
turns =
input("\nPlease enter how many tries you would like:\n")

#Convert str to int
turns = int(turns)

print("\nHello " + name, "Time to play hangman!")
print("\nWord Letter count is... ")
print(len(word_selection))

print("")

#wait for 1 second

time.sleep(1)
time.sleep(
2.0)
print("Game started...")


#here we set the secret
word = word_selection

#creates an variable with an empty value
guesses = ''

#determine the number of turns
turns = turns

# Create a while loop

#check if the turns are more than zero
while turns > 0:

# make a counter that starts with zero
failed = 0

# for every character in secret_word
for char in word:

# see if the character is in the players guess
if char in guesses:

# print then out the character
print(char)

else:

# if not found, print a dash
print("-")

# and increase the failed counter with one
failed += 1

# if failed is equal to zero

# print You Won
if failed == 0:
print("\nWell Done", name, "You won!")
print(" \nThe word was", word_selection)

# exit the script
break

print

# ask the user go guess a character
guess = input("\nGuess a character:")

# set the players guess to guesses
guesses += guess

# if the guess is not found in the secret word
if guess not in word:

# turns counter decreases with 1
turns -= 1

# print wrong
print("\nWrong")

# how many turns are left
print("\nYou have", + turns, 'more guesses')

# if the turns are equal to zero
if turns == 0:

# print "You Loose"
print("\nYou Lose! Better luck next time", name)
print("\nThe word was", word_selection)