Hangman Game Project in Python | 100 Days Of Code | AJ Blogs

 



ASCII Art Code:

stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''','''
+---+
| |
O |
/|\ |
|
|
=========''','''
+---+
| |
O |
/| |
|
|
=========''','''
+---+
| |
O |
| |
|
|
=========''','''
+---+
| |
O |
|
|
|
=========''','''
+---+
| |
|
|
|
|
=========''']
logo = """
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/ """

Words List Code: Click Here

Final Functional Code:

import random

#If words are stored in different File
  #import hangman_words
  #chosen_word = random.choice(hangman_words.word_list)
chosen_word = random.choice(word_list)

#Logo
#If Logo is stored in different file
#from hangman_art import logo
print(logo)

#TestingCode
#print("Chosen_Word ",chosen_word)

#No. of Lives
lives = 6

display = []
word_length = len(chosen_word)
for _ in range(word_length):
  display += "_"
print(display)

end_of_game = False
while not end_of_game:
  guess = input("Guess a Letter ").lower()

  if guess in display:
    print(f"You've  already guessed {guess}")

  for position in range(word_length):
    letter = chosen_word[position]
    if letter == guess:
      display[position] = letter

  #If guess is not a letter in chosen _word
  #Reduce Lives by 1
  if guess not in chosen_word:
    print(f"You guessed {guess}, that's not in the word. You lose a life")
    lives -= 1
    if lives == 0:
      end_of_game = True
      print("You Lose")
  
  #Join all the elements in the list and turn it into a string.
  print(f"{' '.join(display)}")

  #Check if user has got all letters.
  if "_" not in display:
    end_of_game = True
    print("You Win")

  #Print the ASCII art from stages that corresponds to the current number of 'lives' the user has remaining

  #For stages stored in different file 
  #from hangman_art import stages
  print(stages[lives])

Output:

 _                                             
| |                                            
| |__   __ _ _ __   __ _ _ __ ___   __ _ _ __  
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                    __/ |                      
                   |___/                       
['_', '_', '_', '_', '_', '_', '_', '_', '_', '_']
Guess a Letter a
_ _ a _ _ _ _ a _ _

  +---+
  |   |
      |
      |
      |
      |
=========
Guess a Letter h
You guessed h, that's not in the word. You lose a life
_ _ a _ _ _ _ a _ _

  +---+
  |   |
  O   |
      |
      |
      |
=========
Guess a Letter s
_ _ a _ s _ _ a _ _

  +---+
  |   |
  O   |
      |
      |
      |
=========
Guess a Letter i
You guessed i, that's not in the word. You lose a life
_ _ a _ s _ _ a _ _

  +---+
  |   |
  O   |
  |   |
      |
      |
=========
Guess a Letter o
You guessed o, that's not in the word. You lose a life
_ _ a _ s _ _ a _ _

  +---+
  |   |
  O   |
 /|   |
      |
      |
=========
Guess a Letter t
t _ a _ s _ _ a _ t

  +---+
  |   |
  O   |
 /|   |
      |
      |
=========
Guess a Letter i
You guessed i, that's not in the word. You lose a life
t _ a _ s _ _ a _ t

  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
Guess a Letter h
You guessed h, that's not in the word. You lose a life
t _ a _ s _ _ a _ t

  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
Guess a Letter m
You guessed m, that's not in the word. You lose a life
You Lose
t _ a _ s _ _ a _ t

  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========


More Python Programs - Click Here

AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page